Batch file for loop through switch-like arguments?

前端 未结 2 1568
谎友^
谎友^ 2021-02-14 08:47

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

2条回答
  •  你的背包
    2021-02-14 09:42

    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.

提交回复
热议问题