Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to call a batch file from another batch file with parameters that contain spaces?

Tags:

batch-file

I have 2 (maybe more in future) layers of batch files that are making my life easier up until the point I tried to add paths with spaces in them.

Batch file 1:

@echo off
set thinga=c:\final build
set thingb=\\server\deployment for final buil

echo.
echo thing a: %thinga%
echo thing b: %thingb%
echo.

call lala.bat "%thinga%" "%thingb%"

Batch file 2 (lala.bat):

@echo off

echo.
echo. Param 1 %1
echo. Param 2 %2
echo.
set BASE=%1
set TARGET=%2
echo. Want to run:
echo.  doSomethingOnBaseFolder %BASE%
echo.  doSomethingOnBaseSubFolder "%BASE%\bin\release\*" "%TARGET%\"
echo.

The output of this is:

doSomethingOnBaseSubFolder ""c:\final build"\bin\release\*" ""\\server\deployment for final buil"\"

But I want the output to be

doSomethingOnBaseSubFolder "c:\final build\bin\release\*" "\\server\deployment for final buil\"

Is there no way to escape the space in any other way?

like image 717
My Other Me Avatar asked Nov 19 '10 11:11

My Other Me


1 Answers

Use this syntax:

set VAR="%~1"

The %~1 is the first parameter without quotes, then put quotes around it to correctly handle paths with spaces in them. Like that you are always on the safe side.

like image 109
Helge Klein Avatar answered Oct 16 '22 05:10

Helge Klein