I'm trying to loop through the arguments that I am passing to a batch file. Based on the argument, I want to set a variable flag true or false for use later in the script
So my command is "myscript.bat /u /p /s"
And my code is:
FOR /f %%a IN ("%*") DO (
IF /I "%%a"=="/u" SET UPDATE=Y
IF /I "%%a"=="/p" SET PRIMARY=Y
IF /I "%%a"=="/s" SET SECONDARY=Y
)
It only works if i have a single argument, which tells me that it is getting the entire list of arguments as a single argument. I've tried "delims= " but to no avail. Any thoughts on getting each spaced argument?
What about adding a value to one of the params?
myscript.bat /u /p /d TEST /s
:loop
IF "%~1"=="" GOTO cont
IF /I "%~1"=="/u" SET UPDATE=Y
IF /I "%~1"=="/p" SET PRIMARY=Y
IF /I "%~1"=="/s" SET SECONDARY=Y
IF /I "%~1"=="/d" SHIFT & SET DISTRO="%~1"
SHIFT & GOTO loop
:cont
But the SHIFT that comes inline with the last IF doesn't actually shift anything. DISTRO ends up being "/d" instead of "TEST"
You're not too far off on your original piece, and since I dislike GOTO loops, I thought I'd post this:
FOR %%a IN (%*) DO (
IF /I "%%a"=="/u" SET UPDATE=Y
IF /I "%%a"=="/p" SET PRIMARY=Y
IF /I "%%a"=="/s" SET SECONDARY=Y
)
The reason it was only working with one parameter is the over-use of quotes. By putting %* in quotes you were making the entire commandline one single token. also, the /F variant of FOR isn't what you were looking for either. The documentation available from FOR /? should help clear things up.