Pass a path with space to a batch file as a parameter

60,942

To pass parameters with spaces you need to quote the parameter, then you can remove the quotes using %~1.

So the full script would look like

SET var5=C:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\bin\x86_amd64\
CALL scripts\vc64.bat "%var5%"

SET var6=%~1vcvarsx86_amd64.bat
CALL %var6%
Share:
60,942

Related videos on Youtube

user565739
Author by

user565739

Updated on September 18, 2022

Comments

  • user565739
    user565739 over 1 year

    In first.bat, I use

    var5=C:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\bin\x86_amd64\
    CALL scripts\vc64.bat %var5%
    

    And in scripts\vc64.bat, I use

    SET var6=%1vcvarsx86_amd64.bat
    CALL %var6%
    

    But I get : 'C:\Programvcvarsx86_amd64.bat' is not internal or external command..... error.

    If the path assigned to var5 has no space, then it is fine.

    I tried several combination of quotations and %1vcvarsx86_amd64.bat, but no change.

    How do I make it works with path with spaces?


    To be more precise, suppose it is C:\a b c\

    In first.bat: 
         SET var6=C:\a b c\  =====>  '"C:\avcvarsx86_amd64.bat"' is not.....`
         SET var6="C:\a b c\" ====>   There should not be a b
         SET var6=C:\a\ b\ c\ ====>    Can't find the specified path
         SET var6=C:\a b c\ + Using "%var6" ====> There should not be a b
    

    Update: Here is an example. Change a b to ab works.

    call.bat in C:\

    @ECHO OFF
    
    SET var5=C:\a b\
    CALL C:\1.bat "%var5%"
    
    pause
    

    1.bat in C:\

    @ECHO OFF
    
    SET var6=%~1Test.bat
    CALL %var6%
    
    RMDIR /S C:\NoWorry
    

    Test.bat in C:\a b Test.bat in C:\ab

    @ECHO OFF
    
    RMDIR /S C:\ThereIsNoSuchFolder
    
    • feeela
      feeela over 11 years
      Have you tried escaping the single spaces with backslashes (bla\ blub) or enclosing the whole path in quotes ("bla blub")?
  • user565739
    user565739 over 11 years
    This gives me 1> There should not be Files。
  • Bali C
    Bali C over 11 years
    It works fine for me, I was missing a set in the answer, try now.
  • user565739
    user565739 over 11 years
    Using "%var5" just give errors like There should not be..... I don't know why.
  • user565739
    user565739 over 11 years
    I mean use quote with %(variable name) just gives me error and it can't call vc64.bat. Without the quotes, it can call vc64.bat, but the parameter is not good.
  • Bali C
    Bali C over 11 years
    You need to use "%var5%" with a % sign at each side.
  • user565739
    user565739 over 11 years
    Sorry, it is just a typo here. In the bat, no such mistake but don't work. I may just upload the file and you may see what's the problem.
  • Bali C
    Bali C over 11 years
    Yeah sure, just edit your question and post all your scripts and I can have a look. Just put Update or something in the question so I know what's new :)
  • Bali C
    Bali C over 11 years
    It works fine for me, I get C:\a b\Test.bat in %var6%, so I'm not sure why yours won't work, sorry!
  • user565739
    user565739 over 11 years
    Never mind, I found the answer. To make the call successful, it should be "C:\a b\Test.bat" or "C:\a b"\Test.bat
  • Arturas M
    Arturas M over 4 years
    It does not work. Or at least not in all cases. There are exceptions when the arguments themselves contain quotes, in that case the arguments may not have extra quotes, otherwise they get passed without quotes and your application won't work.