Set a path variable with spaces in the path in a Windows .cmd file or batch file

前端 未结 9 740
孤独总比滥情好
孤独总比滥情好 2020-12-02 05:43

I\'m new to script writing and can\'t get this one to work. I could if I moved the files to a path without a space in it, but I\'d like it to work with the space if it coul

相关标签:
9条回答
  • 2020-12-02 06:27

    There are two options here. First, you can store the path unquoted and just quote it later:

    set MyPath=C:\Program Files\Foo
    "%MyPath%\foo with spaces.exe" something
    

    Another option you could use is a subroutine which alles for un-quoting strings (but in this case it's actually not a very good idea since you're adding quotes, stripping them away and re-adding them again without benefit):

    set MyPath="C:\Program Files\Foo"
    call :foo %MyPath%
    goto :eof
    
    :foo
    "%~1\foo.exe"
    goto :eof
    

    The %~1 removes quotation marks around the argument. This comes in handy when passing folder names around quoted but, as said before, in this particular case it's not the best idea :-)

    0 讨论(0)
  • 2020-12-02 06:28

    Try this;

    1. create a variable as below

      SET "SolutionDir=C:\Test projects\Automation tests\bin\Debug"**
      
    2. Then replace the path with variable. Make sure to add quotes for starts and end

      vstest.console.exe "%SolutionDir%\Automation.Specs.dll"
      
    0 讨论(0)
  • 2020-12-02 06:33

    also just try adding double slashes like this works for me only

    set dir="C:\\1. Some Folder\\Some Other Folder\\Just Because"

    @echo on MKDIR %dir%

    OMG after posting they removed the second \ in my post so if you open my comment and it shows three you should read them as two......

    0 讨论(0)
  • 2020-12-02 06:37

    Try something like this:

    SET MY_PATH=C:\Folder with a space
    
    "%MY_PATH%\MyProgram.exe" /switch1 /switch2
    
    0 讨论(0)
  • 2020-12-02 06:41

    I always place the path in double quotes when I am creating a .bat file. (I just added the PAUSE so it wont close the screen.)

    For example:

    "C:\Program Files\PageTech\PCLReader64_131\PCLReader64.exe"
    PAUSE
    
    0 讨论(0)
  • 2020-12-02 06:43

    The proper way to do this is like so:

    @ECHO off
    SET MY_PATH=M:\Dir\^
    With Spaces\Sub Folder^
    \Dir\Folder
    :: calls M:\Dir\With Spaces\Sub Folder\Dir\Folder\hello.bat
    CALL "%MY_PATH%\hello.bat"
    pause
    
    0 讨论(0)
提交回复
热议问题