How do I pass multiple variables from an Excel file to a batch file

后端 未结 2 1633
陌清茗
陌清茗 2021-01-29 01:46

I am currently able to pass one argument from my Excel file to my batch file using the following:

 filepath = \"C:\\Users\\agaron\\Desktop\\batchmaster\\batchfi         


        
2条回答
  •  星月不相逢
    2021-01-29 02:47

    To provide multiple parameters to a batch file, separate them with spaces. If a parameter contains spaces on its own, enclose it in "" (this is also required for some other characters like ,, ;, =; use quotes also for the batch file path in case; the quotes might also be used for every parameter):

    "C:\Users\agaron\Desktop\batchmaster\batchfiles\batchfiletest.bat" param1 param2 "param3 with spaces" ...
    

    To access the parameters in the batch file, use the %# syntax, were # represents a single decimal digit (see call /? for details). Note that %0 returns the path of the batch file itself. To remove potential surrounding "", use %~#:

    echo This batch file: %~0
    echo First  parameter: %~1
    echo Second parameter: %~2
    echo Third  parameter: %~3
    

    With the shift command, you shift the assignment of the %# numbers, for instance:

    echo This batch file: %~0
    shift
    echo First  parameter: %~0
    echo Second parameter: %~1
    shift
    echo Third  parameter: %~1
    

    As you might have noticed, you cannot access the batch file path anymore after the first shift; the next shifts prevents the first parameter from being accessed, and so on (type shift /? for more information).

    However, shift lets you access more than 9 parameters. For example, when calling the batch file with a command line like:

    "C:\Users\agaron\Desktop\batchmaster\batchfiles\batchfiletest.bat" param1 param2 param3 ... param9 param10
    

    you can access the parameters like:

    echo This batch file: %~0
    echo Parameter  1: %~1
    shift
    echo Parameter  2: %~1
    shift
    echo Parameter  3: %~1
    :: ...
    shift
    echo Parameter  9: %~1
    shift
    echo Parameter 10: %~1
    

    As you can see, shift provides the possibility to access even more then 9 parameters.


    Finally, let us call the batch file via the VBA code:

    filepath = Chr(&H22) & "C:\Users\agaron\Desktop\batchmaster\batchfiles\batchfiletest.bat" & Chr(&H22) _
        & " " & variable_containing_param1 _
        & " " & "param2" _
        & " " & Chr(&H22) & "param3 with spaces" & Chr(&H22)
    Call Shell(filepath, vbNormalFocus)
    

提交回复
热议问题