Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adding flags to a batch script

I'm writing a Batch script that will run on a directory. I want to be able to add a flag (such as -r or /r or something like that) that will make the script run the folder tree instead of the single directory. Is it possible to add flag options using batch?

Thanks

like image 455
ZackG Avatar asked Feb 28 '13 18:02

ZackG


1 Answers

Certainly it's possible. Command line parameters are passed in to your batch file as %1, %2, etc. (%0 is the name of the batch file.)

IF "%1"=="/r" ECHO You passed the /r flag.

Use SHIFT to drop the first argument and move all the others over by one. You can use that to get a bit fancier if you want to allow that /r to be anywhere in the command line. For example:

:processargs
SET ARG=%1
IF DEFINED ARG (
    IF "%ARG%"=="/r" ECHO You passed the /r flag.
    SHIFT
    GOTO processargs
)

Or use %* (which expands to the entire argument list) in a FOR loop like this:

FOR %%A IN (%*) DO (
    IF "%%A"=="/r" ECHO You passed the /r flag.
)
like image 50
Nate Hekman Avatar answered Oct 14 '22 12:10

Nate Hekman