Pass variable from Batch to VBS

前端 未结 3 1301
甜味超标
甜味超标 2020-12-21 11:24

Hi I am trying to pass a user input variable from a BAT to VBS script

Im sure this can be done in VBS but the user \"Filename\" input is used also later in the BAT f

相关标签:
3条回答
  • 2020-12-21 11:59

    almost everything can be done with vbs. you don't need a batch

    to get user input from vbs, you can use stdin. copy from here.

    ' Read a single line into a variable
    Dim strMyName
    WScript.StdOut.Write("Enter Your full Name>")
    WScript.StdIn.Read(0)
    strMyName = WScript.StdIn.ReadLine()
    WScript.StdOut.Write(strMyName)
    
    0 讨论(0)
  • 2020-12-21 12:05
    WScript.CreateObject("WScript.Shell").ExpandEnvironmentStrings("%FileName%")
    
    0 讨论(0)
  • 2020-12-21 12:10

    I wouldn't recommend passing information from one script to another via environment variables. I suppose you're running the VBScript from the batch script? In that case you could simply pass the filename as an argument to the VBScript:

    set /p "FileName= Enter Filename Including Extention (e.g. test.xlsx): "
    cscript.exe //NoLogo C:\path\to\your.vbs "%FileName%"
    

    In the VBScript you process the argument like this:

    filename = WScript.Arguments.Unnamed(0)
    

    You could also use a name argument:

    set /p "FileName= Enter Filename Including Extention (e.g. test.xlsx): "
    cscript.exe //NoLogo C:\path\to\your.vbs /filename:"%FileName%"
    

    with the argument evaluation in your VBScript looking like this:

    filename = WScript.Arguments.Named("filename")
    
    0 讨论(0)
提交回复
热议问题