【问题标题】:How to use variable of batch file into VB script file如何将批处理文件的变量用于VB脚本文件
【发布时间】:2017-01-22 05:50:49
【问题描述】:

我创建了批处理文件,其中包括批处理文件的代码以及 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 代码并使用它们

非常感谢您提前...

【问题讨论】:

    标签: batch-file vbscript


    【解决方案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
    

    【讨论】:

      【解决方案2】:

      这会生成正确的消息框:

      @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,以便能够用变量中的变量替换值。 我还必须创建另一个临时文件,然后将其删除。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2013-12-27
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-11-19
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多