r3ap3r
r3ap3r

Reputation: 2815

how to call a batch file with spaces in its path from another batch file and return control to the parent batch file

I am trying to call a batch file from another batch file and after the second batch file executes , the control should be returned to the first batch file and it should resume execution. Currently i am using the following command in my parent batch file:

call "cmd /c start /b %ROOT_HOME%\folder1\bin\bat1.bat"

This works fine as long as %ROOT_HOME% has a path which doesn't have spaces in it.

The above command fails if the path contains spaces in it.

I have tried every combination of cmd, start, call but still unable to achieve the desired result.

Also bat1.bat doesn't have exit inside it and it can't be modified. So using call alone only executes the child batch file and doesn't return to the parent batch file.

Upvotes: 4

Views: 6763

Answers (3)

npocmaka
npocmaka

Reputation: 57262

what about if you try with short path:

for /f %%P in (%ROOT_HOME%) do set SHORT_ROOT_HOME=%%~sP
call "cmd /c start /b  %SHORT_ROOT_HOME%\folder1\bin\bat1.bat"

Upvotes: 2

Joey
Joey

Reputation: 354586

You don't need call there at all, since you're spawning the new batch via cmd anyway:

start "" /b "%ROOT_HOME%\folder1\bin\bat1.bat"

Although I wonder why you'd use start /b unless you're worried about environment pollution (which can be reverted easily by using setlocal/endlocal around the call), so call should suffice as well:

call "%ROOT_HOME%\folder1\bin\bat1.bat"

As a general rule: More layers of parsing and execution around something make things only more complicated and rarely makes problems go away. Cf. overuse of iex in PowerShell.

Upvotes: 4

Bali C
Bali C

Reputation: 31231

It should be as simple as this

call cmd /c start /b "" "%ROOT_HOME%\folder1\bin\bat1.bat"

Upvotes: 1

Related Questions