How to use a variable in a path to execute a .bat file


  1. Posts : 6
    Windows
       #1

    How to use a variable in a path to execute a .bat file


    Hi,

    I have a requirement where I need to use a variable instead of a full path when I call a bat file.

    Example:
    var path = "C://xyz/..."
    Sys.OleObject("WScript.Shell").Run("cmd /c "" path + myfile.bat"");

    The above line doesn't seem to work with 'path' variable but works fine when I add the full path.

    Can anyone help with this please.

    Thanks
      My Computer


  2. Posts : 18,032
    Win 10 Pro 64-bit v1909 - Build 18363 Custom ISO Install
       #2

    Hello @TestQA,

    TestQA said:
    Example:
    var path = "C://xyz/..."
    Sys.OleObject("WScript.Shell").Run("cmd /c "" path + myfile.bat"")
    You need to use set. Something like this EXAMPLE . . .

    Code:
    
    @echo off
    set "Var_Path=C:/xyz/..."
    echo "%Var_Path%"
    pause & exit

    I hope this helps.
      My Computer


  3. Posts : 15,940
    Windows10
       #3

    TestQA said:
    Hi,

    I have a requirement where I need to use a variable instead of a full path when I call a bat file.

    Example:
    var path = "C://xyz/..."
    Sys.OleObject("WScript.Shell").Run("cmd /c "" path + myfile.bat"");

    The above line doesn't seem to work with 'path' variable but works fine when I add the full path.

    Can anyone help with this please.

    Thanks
    Variables normally need %signs when used e.g.

    %Path%
      My Computer


  4. Posts : 6
    Windows
    Thread Starter
       #4

    Thanks for replies. I should have mentioned that the line Sys.OleObject("WScript.Shell").Run("cmd /c "" path + myfile.bat"") is not in bat file but it's a javascript code that calls .bat file.
    I am sorry if its not relevant. I think it has something to do with using "" correctly.
      My Computer


  5. Posts : 841
    Windows 10/11
       #5

    You're missing a + and it's VBScript where you double up double quotes to "escape" them. In JavaScript (or JScript) the escape character is the backslash. However, JavaScript also allows single quotes or double quotes around a string so you don't have to escape the double quotes. Therefore...

    "cmd /c "" path + myfile.bat""

    should be changed to....

    'cmd /c "' + path + 'myfile.bat"'

    Tip: Whenever dealing with paths (especially ones containing spaces) always preview your string using alert (if you're in a browser) or wscript.echo if you're outside a browser. Then you will see clearly if you got the command line correct before attempting to run it.
      My Computer


 

  Related Discussions
Our Sites
Site Links
About Us
Windows 10 Forums is an independent web site and has not been authorized, sponsored, or otherwise approved by Microsoft Corporation. "Windows 10" and related materials are trademarks of Microsoft Corp.

© Designer Media Ltd
All times are GMT -5. The time now is 08:33.
Find Us




Windows 10 Forums