3

I want to run ONE batch file, that starts 4 other batch files. Problem: Only the first CMD file gets executed.

I looked at a similar questions posted here: How to run multiple .BAT files within a .BAT file
And here: How do I launch multiple batch files from one batch file with dependency?

The difference with my question is that i pass along a RootPath for each CMD file.

Examples given in other posts: 
call msbuild.bat
call unit-tests.bat
call deploy.bat

My code:
SET RootPath="G:\Dev Folder\Framework\MainFolder\SubFolder\JOBS\"
CALL %RootPath%Account.CMD
CALL %RootPath%Customer.CMD
CALL %RootPath%Contract.CMD
CALL %RootPath%Location.CMD

This Master.CMD file containing the code is not in the same directory as the Account/ Customer/ Contract/ Location cmd files, that's why i'm passing an absolute path. The first command runs just fine. And then i get the error:

'..\Customer.CMD ' is not recognized as en internal or external command, operable program or batch file.
'..\Contract.CMD ' is not recognized as en internal or external command, operable program or batch file.
'..\Location.CMD ' is not recognized as en internal or external command, operable program or batch file.

I'm not able to deduce a solution from the other Batch posts on stackoverflow. I'm new to this language, please excuse me if i've overlooked something.

Any and all help is welcome.

Community
  • 1
  • 1
NetFlash
  • 65
  • 4
  • 12
  • RootPath has a space inside. Try `CALL "%RootPath%Account.CMD"` with quotes. – Thomas Weller May 13 '14 at 11:58
  • 1
    this looks like your `ACCOUNT.CMD` resets `RootPath`.Env variables are global, unless you isolate them with `setlocal` – wmz May 13 '14 at 12:01
  • RootPath value has quotations. Moreover adding quotations to **CALL "%RootPath%Account.CMD"** doesn't seem to fix the problem – NetFlash May 13 '14 at 14:10
  • Thanks @wmz. Credits go to you and a colleague from work. Your answers made me re-assess. Can you up your comment as answer? The path was being overwritten in a child package..by the use of keyword 'call' the executing context is inherited by the child CMD's. So when the Account.CMD changed the 'RootPath' it reflected back into the master, where the next package was called as '..\Customer.cmd' instead of G:\[...]\Customer.cmd Solution was to use a different variable name (MasterPath) in the Master.CMD – NetFlash May 13 '14 at 15:12

1 Answers1

1

This looks like your Account.cmd resets RootPath. Env variables are (session) global, unless you isolate them with setlocal. To resolve you could change names (as you did) or use setlocal in your batch files. It's generally good practice anyway as it helps to avoid unexpected/unwanted side effects. Here is a quote from help setlocal:

Begins localization of environment changes in a batch file. Environment changes made after SETLOCAL has been issued are local to the batch file. ENDLOCAL must be issued to restore the previous settings. When the end of a batch script is reached, an implied ENDLOCAL is executed for any outstanding SETLOCAL commands issued by that batch script.

wmz
  • 3,525
  • 1
  • 12
  • 21