When testing for a particular letter in a batch file option, I use code like:
set opts=%2
if not "%opts:t=%" == "%opts%" goto :handle-t-option
However, if %2 is empty, which means opts is empty, then I get a bizarre syntax error, where it seems that the expansion simply doesn't work.
As a workaround, I set opts to include a character not in the set of legal options, just to avoid the syntax error, but thought I'd ask if I'm missing something, or if not, have a question that might help others realize that this is a bizarre corner case.
"%opts:t=%"makes no sense. Please explain what do you want to achieve. First of all, what information to do you want to pass as%2?set optsinstruction, add a new lineif defined opts goto somewhereaftertheifnot. Tip: Useset "var=value"for setting string values - this avoids problems caused by trailing spaces. The colon in agotostatement is not required.set opts=results always in a deletion of the environment variableoptson existing at all. See the Microsoft documentation about Environment Variables. The simple solution is inserting below the command lineset opts=%2the command lineif not defined opts goto Labelto continue the processing of the batch file on no option defined below the line with:Label.set opts=%2is not good in case of a user of the batch file passes for whatever reason"Development & Test 100% (!)"as second argument string to the batch file. That argument string causes an execution ofif not ""Developmen & es 100% (!)"" == ""Development & Test 100% (!)"" goto :handle-t-optionwhich results in the error message:& was unexpected at this time.Better would beset "opts=%~2"with syntactically correctif not "Developmen & es 100% (!)" == "Development & Test 100% (!)" goto :handle-t-optionand therefore no error message on typical argument strings.