none
Variables in Wsh Shell and cmd.exe commands RRS feed

  • Question

  • In Excel, I would use variables in a wsh.run command (it should launch a batch file), instead of local path, but I got an error in the dos console: "The file name, directory name or volume label syntax is incorrect".

    Here the code that works:

    ErrorCode = wsh.Run("cmd.exe /k cd /d G:\Projects\Tests && G:\Projects\Tests\run.bat", windowStyle, waitOnReturn)

    I would use two variables, instead: wkPath and fBatchFile.

    wkPath = Application.ActiveWorkbook.Path
    fBatchFile = wkPath & "\run.bat"

    But when I try to use:

    ErrorCode = wsh.Run("cmd.exe /k cd /d " & wkPath & fBatchFile & "", windowStyle, waitOnReturn)

    I got a console error: ""The file name, directory name or volume label syntax is incorrect".

    Any help would be greatly appreciated!



    • Edited by giangis Friday, November 15, 2019 5:02 PM
    Friday, November 15, 2019 5:00 PM

Answers

  • ErrorCode = wsh.Run("cmd.exe /k cd /d " & wkPath & " && " & fBatchFile, windowStyle, waitOnReturn)

    Regards, Hans Vogelaar (http://www.eileenslounge.com)

    • Marked as answer by giangis Friday, November 15, 2019 6:18 PM
    Friday, November 15, 2019 5:16 PM

All replies

  • ErrorCode = wsh.Run("cmd.exe /k cd /d " & wkPath & " && " & fBatchFile, windowStyle, waitOnReturn)

    Regards, Hans Vogelaar (http://www.eileenslounge.com)

    • Marked as answer by giangis Friday, November 15, 2019 6:18 PM
    Friday, November 15, 2019 5:16 PM
  • Thank you very much, Hans. It works!

    Out of curiosity: why if I execute the code with cmd.exe /k parameter, when I close manually the prompt window, I get an "Overflow" error from VBA?

    Friday, November 15, 2019 5:47 PM
  • I'm afraid I don't know, sorry.

    Regards, Hans Vogelaar (http://www.eileenslounge.com)

    Friday, November 15, 2019 7:23 PM
  • No problem, Hans.

    I found this discussion, but I'm not able to know much.
    https://stackoverflow.com/questions/38876518/running-command-com-or-cmd-exe-from-vba-causing-problems-with-msg-command?rq=1

    I write here the link only for future reference: maybe it could be useful.

    Cheers.

    Friday, November 15, 2019 8:36 PM