VBScript:如何通过调用另一个脚本文件来更改变量值?
这就是我这些天在文件之间传递论点的方式。
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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
选项1:
将文件保持原样,并在您的子的末尾添加回声:
2.VBS:
1.VBS
通过更改“ .run ”的方法读取输出。 “
选项2:
使用“ 2.vbs ”文件作为功能库,然后将 sub 更改为函数:
1.VBS
2.VBS
Option 1:
keep the files as they are, and add an echo at the end of your sub:
2.vbs:
1.vbs
Read the output by changing the method ".Run" to ".Exec"
Option 2:
Using the "2.vbs" file as a function library and changing the sub to function:
1.vbs
2.vbs