0

I have one master batch file

Master.bat which called 3 files first.bat , second.bat , third.bat

Now I want to pass parameters to Master.bat and based on parameter want to execute internal statements for example If I passed Master.bat first then Master.bat should execute only call first.bat not others.

Master.bat

call "first.bat" 

call "second.bat" 

call "third.bat"

How can I achieve this? If multiple parameters are also passed?How can I loop though? answer I accepted works fine for me but When I pass extra other parameters it fails :( like below Master.bat first,second parameter1 parameter2 now When I try to execute above it only exceutes first.bat Code for Master.bat is

@echo off
:LOOP
if "%1"=="" goto ENDLOOP
call "%1" %3 %2 
shift
goto LOOP
:ENDLOOP

%1 should take first,second %2 should take parameter1 %2 should take parameter2

Neo
  • 15,491
  • 59
  • 215
  • 405
  • Seems like it would be just as easy to execute the batch files you want by just chaining them together instead of using a main batch file to execute them. Less to type! `C:\BatchFiles>first & second` or `C:\BatchFiles>first & third` – Squashman Dec 01 '15 at 15:48
  • Possible duplicate of [how to pass comma separated parameter to batch file and use if else in batch file?](http://stackoverflow.com/questions/33955749/how-to-pass-comma-separated-parameter-to-batch-file-and-use-if-else-in-batch-fil) – Squashman Dec 01 '15 at 16:34
  • 1
    Trying to understand the relation between your previous question and this one. Again you can just chain the commands instead of having a separate batch file to do all the shifting. `C:\BatchFiles>first a b & second c d` – Squashman Dec 01 '15 at 19:29

2 Answers2

2

The command you are looking for is shift:

@echo off
:LOOP
if "%1"=="" goto ENDLOOP
call "%1"
shift
goto LOOP
:ENDLOOP

The first passed parameter is always stored in %1, the second one in %2 the 3rd in %3 and so on. The shift command moves the index to the right. The old %1 is lost, %2 becomes %1, %3 becomes %2 and so on. This way you can simply iterate over all passed parameters until you've shifted as often as there are parameters. Then %1 becomes empty and you break the loop with goto ENDLOOP.

[EDIT] According to your comment here is how to do what you are trying:

@echo off
:LOOP
if "%1"=="" goto ENDLOOP
call "%1" %2 %3
shift
shift
shift
goto LOOP
:ENDLOOP

If you are calling master.bat a.bat b c x.bat y z this will result in calling a.bat b c and x.bat y z.

MichaelS
  • 5,941
  • 6
  • 31
  • 46
  • where to use if statement? like if parameter is `first` then execute `first.bat` or what If i passed more than one parameters like `Master.bat first,second` it means execute both first.bat and second.bat – Neo Dec 01 '15 at 11:42
  • When you enter the loot for the first time first.bat is stored in %1 so it's being executed by `call %1`. Afer the first iteration %1 is second.bat so second.bat is being executed. Just don't use `,` as a separator. If you want to use `,` instead of spaces to separate your parameters, the whole file `first.bat,second.bat` will be interpreted as one single parameter. In this case you will need to split it by `,` but this is a whole different question. – MichaelS Dec 01 '15 at 11:47
  • this works for me but not for multiple parameters :( i tried like `@echo off :LOOP if "%1"=="" goto ENDLOOP call "%1" %2 %3 shift goto LOOP :ENDLOOP` called Master.bat first,second param111 param222 – Neo Dec 01 '15 at 12:13
  • 1
    Please update your question. The points you are asking for are not stated in the question text itself. – MichaelS Dec 01 '15 at 12:19
  • This solution chokes on `"%1"==""` when the argument is a filename that contains a space. – snips-n-snails Jul 13 '20 at 15:35
2

Your batch file could do this in more than one way:

  • the most common is to call successively the first parameter %1 and use shift to discard that, then if to check if there are more parameters and goto to return to the top of a loop.

    :loop call "%1" shift if not "x%1"=="x' goto loop

  • alternatively, your script could process the command-line with for:

    for %%n in ( %* ) do call "%%n"

Based on comment, OP may also have in mind a case where the parameters are comma-separated lists. The for command can do this (with a more complicated example) using the delims= option, by constructing a variable to use as the built-up command, adding the split-off tokens to it and finally executing the resulting built-up command. That probably should be a separate question. For discussion purposes, see How do I get a for loop to work with a comma delimited string?

Community
  • 1
  • 1
Thomas Dickey
  • 51,086
  • 7
  • 70
  • 105
  • what If i passed more than one parameters like Master.bat first,second it means execute both first.bat and second.bat – Neo Dec 01 '15 at 11:45
  • That wasn't in the original question, but `for` (read the documentation) can step through a list with a specific delimiter). – Thomas Dickey Dec 01 '15 at 11:46
  • @ThomasDickey, I agree with you on just using a basic `FOR` command. It matches the users description perfectly if you ask me. Simple as pie. – Squashman Dec 01 '15 at 14:08