我想将变量从VBScript传递给批处理,但是它不起作用。
我的VBScript:
Dim shell
Set shell = CreateObject("WScript.Shell")
strnaam = InputBox ("naam")
和我的批次:
@echo off
cls
echo %strnaam%
pause
我希望变量strnaam
从我的VBScript到我的批处理。
答案 0 :(得分:3)
最明显的方法是将vbscript作为For /F
命令运行,并将其返回的输出保存为变量:
@Echo Off
:NaamBox
Set "naam="
(Echo WScript.Echo InputBox("Naam:"^))>"%TEMP%\naam.vbs"
For /F Delims^=^ EOL^= %%A In ('CScript //NoLogo "%TEMP%\naam.vbs"')Do Set "naam=%%A"
If Not Defined naam GoTo NaamBox
Del "%TEMP%\naam.vbs"
Echo Uw naam is %naam%
Pause
如果您不喜欢编写,运行然后删除文件的想法,则还可以将VBScript嵌入批处理文件中:
<!-- :
@Echo Off
Echo Typ gelieve uw naam in de popup doos en OK te selecteren
For /F Delims^=^ EOL^= %%A In ('CScript //NoLogo "%~f0?.wsf"')Do Set "naam=%%A"
If Defined naam Echo Uw naam is %naam%&Pause
Exit /B
-->
<Job><Script Language="VBScript">
WScript.Echo InputBox("Naam:")
</Script></Job>
答案 1 :(得分:1)
您只能通过环境变量传递变量:
create_variable.vbs
文件:Dim WshShell, WshEnv
Set WshShell = CreateObject("WScript.Shell")
Set WshEnv = WshShell.Environment("USER") ' can be either SYSTEM, USER, VOLATILE OR PROCESS
' current value
WScript.Echo "Process: NAAM=" & WshEnv.Item("NAAM")
WshEnv("NAAM") = "This text will appear in batch"
WScript.Echo "Process: NAAM=" & WshEnv.Item("NAAM")
Set WshEnv = Nothing
Set WshShell = Nothing
show_vbs_variable.bat
(您必须打开一个新的cmd.exe才能在其中拥有新变量!如果需要更多信息,here是这样就可以了。@echo off
cls
echo %naam%
pause
clearing_variable.vbs
:Dim WshShell, WshEnv
Set WshShell = CreateObject("WScript.Shell")
Set WshEnv = WshShell.Environment("USER") ' can be either SYSTEM, USER, VOLATILE OR PROCESS
' current value
WScript.Echo "Process: NAAM=" & WshEnv.Item("NAAM")
'Deleting the env variable
WshEnv.Remove("NAAM")
WScript.Echo "Process: NAAM=" & WshEnv.Item("NAAM")
Set WshEnv = Nothing
Set WshShell = Nothing