如何将批处理文件的变量用于VB脚本文件

时间:2016-09-14 12:12:41

标签: batch-file vbscript

我创建了批处理文件,其中包括批处理文件的代码以及VBScript的代码。现在我试图将变量值从批处理文件传递给VBScript,但它不起作用。

echo This is batch
set /p Name="Enter Your Name: "
:x=msgbox("You have Entered '" & name & "'" ,0, "Your Title Here")
findstr "^:" "%~sf0">temp.vbs & cscript //nologo temp.vbs & del temp.vbs
echo This is batch again

我得到以下输出:

c:\Users\vshah\Desktop>echo This is batch
This is batch

c:\Users\vshah\Desktop>set /p Name="Enter Your Name: "
Enter Your Name: Vinkesh

c:\Users\vshah\Desktop>findstr "^:" "c:\Users\vshah\Desktop\Print.bat"  1>temp.vbs  & cscript //nologo temp.vbs   & del temp.vbs

c:\Users\vshah\Desktop>echo This is batch again
This is batch again

c:\Users\vshah\Desktop>

In Message Box I am getting message only -- You have Entered "
Not getting variable output

请帮助我将变量从批处理代码传递到VBScript代码并使用它们

非常感谢你提前...

2 个答案:

答案 0 :(得分:1)

这会生成正确的msgbox:

@echo off
setlocal enabledelayedexpansion
echo This is batch
set /p Name="Enter Your Name: "
:x=msgbox("You have Entered '__name__'" ,0, "Your Title Here")
findstr "^:" "%~sf0"> %TEMP%\str
set /p vbl=<%TEMP%\str
del %TEMP%\str >NUL
set vbl=%vbl:__name__=!name!%
rem remove the first colon
echo %vbl:~1% 1>temp.vbs  & cscript //nologo temp.vbs   & del temp.vbs

我使用了一个模板(__name__)和enabledelayedexpansion,以便能够用变量中的变量替换值。 我也必须创建另一个临时文件,然后删除。

答案 1 :(得分:1)

只需改变一下:

:x=msgbox("You have Entered '" & name & "'" ,0, "Your Title Here")
findstr "^:" "%~sf0">temp.vbs & cscript //nologo temp.vbs & del temp.vbs

进入这个:

echo x=msgbox("You have Entered '%name%'" ,0, "Your Title Here")>temp.vbs
cscript //nologo temp.vbs & del temp.vbs

请注意,您并没有真正以这种方式传递变量,而是创建一个包含该变量的文字值的临时脚本。如果您希望VBScript实际使用变量,则需要将代码更改为:

echo name=WScript.Arguments.Unnamed(0)>temp.vbs
echo x=msgbox("You have Entered '" ^& name ^& "'" ,0, "Your Title Here")>>temp.vbs
cscript //nologo temp.vbs "%name%"
del temp.vbs