VBScript:如何通过调用另一个脚本文件来更改变量值?

发布于 2025-02-04 00:00:06 字数 895 浏览 3 评论 0原文

这就是我这些天在文件之间传递论点的方式。

1.VBS:

Dim MyVar

'1) Assigning some value to MyVar.
MyVar = "foo"

'2) Passing MyVar to 2.vbs.
CreateObject("WScript.Shell").Run _
Chr(34) & "C:" & "\2.vbs" & _
Chr(34) & " " & Chr(34) & MyVar & Chr(34), _
1, True

'4) End point.
MsgBox MyVar

2.VBS:

    If _
    WScript.Arguments.Count > 0 _
    Then
    Call MySub( _
    WScript.Arguments(0))
    End If

Sub MySub(MyVar)

'3) Doing some work with MyVar.
MyVar = "bar"

End Sub

因此,如果所有代码都在1个文件中,并且
如果我必须使用调用mySub而不是createObject(“ wscript.shell”)。运行
- 我成功地将myvar“ foo”转换为“ bar” ,
并在msgbox上获得了后者。

但是,有时我真的想在另一个文件中使用myvar
并能够将其重新(已经运行第一个文件)带回更改。

我只是不知道该怎么做。

This is how I'm passing arguments between files these days.

1.vbs:

Dim MyVar

'1) Assigning some value to MyVar.
MyVar = "foo"

'2) Passing MyVar to 2.vbs.
CreateObject("WScript.Shell").Run _
Chr(34) & "C:" & "\2.vbs" & _
Chr(34) & " " & Chr(34) & MyVar & Chr(34), _
1, True

'4) End point.
MsgBox MyVar

2.vbs:

    If _
    WScript.Arguments.Count > 0 _
    Then
    Call MySub( _
    WScript.Arguments(0))
    End If

Sub MySub(MyVar)

'3) Doing some work with MyVar.
MyVar = "bar"

End Sub

So, if all code was in 1 single file, and
if I had to use Call MySub instead of CreateObject("WScript.Shell").Run
— i'd successfully changed MyVar from "foo" to "bar",
and got the latter on MsgBox.

Yet, sometimes I really want to work with MyVar in another file,
and be able to get it back (to the already running first file) with changes.

I just don't know how to do that properly.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

奶气 2025-02-11 00:00:06

选项1:
将文件保持原样,并在您的子的末尾添加回声:

2.VBS:

'(...)
'3) Doing some work with MyVar.
MyVar = "bar"
Wscript.Echo(MyVar)
'(...)

1.VBS

通过更改“ .run ”的方法读取输出。 “

'(...)
'2) Passing MyVar to 2.vbs.
set oShell = CreateObject("Wscript.Shell")
set Exe = oShell.Exec("cmd /c 2.vbs """&MyVar&""" ")
MyVar = Exe.StdOut.ReadAll
'(...)
'4) End point.
MsgBox MyVar

选项2:
使用“ 2.vbs ”文件作为功能库,然后将 sub 更改为函数:

1.VBS

'Put the import statements at the top of 1.vbs code for easier readability.
Import "2.vbs"
'(...)
MyVar = MyFunction(MyVar)

2.VBS

Function MyFunction(MyVar)
'3) Doing some work with MyVar.
MyFunction = "bar"
end Function

Option 1:
keep the files as they are, and add an echo at the end of your sub:

2.vbs:

'(...)
'3) Doing some work with MyVar.
MyVar = "bar"
Wscript.Echo(MyVar)
'(...)

1.vbs

Read the output by changing the method ".Run" to ".Exec"

'(...)
'2) Passing MyVar to 2.vbs.
set oShell = CreateObject("Wscript.Shell")
set Exe = oShell.Exec("cmd /c 2.vbs """&MyVar&""" ")
MyVar = Exe.StdOut.ReadAll
'(...)
'4) End point.
MsgBox MyVar

Option 2:
Using the "2.vbs" file as a function library and changing the sub to function:

1.vbs

'Put the import statements at the top of 1.vbs code for easier readability.
Import "2.vbs"
'(...)
MyVar = MyFunction(MyVar)

2.vbs

Function MyFunction(MyVar)
'3) Doing some work with MyVar.
MyFunction = "bar"
end Function
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文