I've blogged some tips on DOS before, but here are some more. One of my coworkers (thanks Roey) pointed out several DOS nuances that I found useful - they're the kind of things that are easy to miss:
Use single % for parameters (like %1), double %% for variables (like %myVar%)
While you can use environmental variables using the percent signs like so: %myVar%, parameters only have the first percent, like so: %1. What's tedious is if you make it %1%, you may be able to get away with it in some cases. But if you put two parameters on the same like like %1...%2, then you need to get it straight.
Set operators within an if-block don't update until after the if-block finishes
In a language like C#, you could both update the variable, and then use that variable within the same if-block. Apparently you can't do that in DOS.
|
You need to let the if-block finish first. You can do this by putting the set operation in its own block, either like this:
|
or like this:
|
DOS for-loops in a batch script require an extra '%' in front of the variable.
This will fail:
|
This will work:
|
This last one is especially tedious to debug because if you copy the contents of the script to a command window to run it, then it suddenly works.
No comments:
Post a Comment