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

6

3

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

user565739

Posted 2013-01-21T11:41:44.953

Reputation: 473

Have you tried escaping the single spaces with backslashes (bla\ blub) or enclosing the whole path in quotes ("bla blub")? – feeela – 2013-01-21T11:43:28.310

Answers

8

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%

Bali C

Posted 2013-01-21T11:41:44.953

Reputation: 1 522

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. – Arturas M – 2020-01-29T18:53:43.790

This gives me 1> There should not be Files。 – user565739 – 2013-01-21T11:59:17.163

It works fine for me, I was missing a set in the answer, try now. – Bali C – 2013-01-21T12:18:54.767

Using "%var5" just give errors like There should not be..... I don't know why. – user565739 – 2013-01-21T13:03:15.850

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. – user565739 – 2013-01-21T13:04:42.947

You need to use "%var5%" with a % sign at each side. – Bali C – 2013-01-21T13:07:47.150

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. – user565739 – 2013-01-21T13:19:40.477

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 – 2013-01-21T13:24:49.107

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! – Bali C – 2013-01-21T14:40:58.743

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 – user565739 – 2013-01-21T21:52:27.520