Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Realizing this is a bit of an old question, the responses helped me come up with a solution to testing command line arguments to a batch file; so I wanted to post my solution as well in case anyone else was looking for a similar solution.</p> <p>First thing that I should point out is that I was having trouble getting IF ... ELSE statements to work inside of a FOR ... DO clause. Turns out (thanks to dbenham for inadvertently pointing this out in his examples) the ELSE statement cannot be on a separate line from the closing parens.</p> <p>So instead of this:</p> <pre><code>FOR ... DO ( IF ... ( ) ELSE ( ) ) </code></pre> <p>Which is my preference for readability and aesthetic reasons, you have to do this:</p> <pre><code>FOR ... DO ( IF ... ( ) ELSE ( ) ) </code></pre> <p>Now the ELSE statement doesn't return as an unrecognized command.</p> <p>Finally, here's what I was attempting to do - I wanted to be able to pass several arguments to a batch file in any order, ignoring case, and reporting/failing on undefined arguments passed in. So here's my solution...</p> <pre><code>@ECHO OFF SET ARG1=FALSE SET ARG2=FALSE SET ARG3=FALSE SET ARG4=FALSE SET ARGS=(arg1 Arg1 ARG1 arg2 Arg2 ARG2 arg3 Arg3 ARG3) SET ARG= FOR %%A IN (%*) DO ( SET TRUE= FOR %%B in %ARGS% DO ( IF [%%A] == [%%B] SET TRUE=1 ) IF DEFINED TRUE ( SET %%A=TRUE ) ELSE ( SET ARG=%%A GOTO UNDEFINED ) ) ECHO %ARG1% ECHO %ARG2% ECHO %ARG3% ECHO %ARG4% GOTO END :UNDEFINED ECHO "%ARG%" is not an acceptable argument. GOTO END :END </code></pre> <p>Note, this will only report on the first failed argument. So if the user passes in more than one unacceptable argument, they will only be told about the first until it's corrected, then the second, etc.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload