![]() |
|
|
#1
|
|||
|
|||
|
Parsing Command Line Parameters in DOS Batch Files
I want to develop a batch file to automate creating images using Norton Ghost 2001. Still a rough draft, but here's the relevant code:
Code:
How do I extract the "W" from this parameter? Any help is greatly appreciated. Thanks! |
|
#2
|
|||
|
|||
|
Well, here is the first draft (for DOS v7.10). Please check my syntax, especially red text. Thanks.
Code:
|
|
#3
|
|||
|
|||
|
Command Line Parsing Solution
sburtchin...
In looking for the exact same solution you were looking for, i came across a web page that had some code that would help me. Using some of the ideas you had about working through this and the information i found elsewhere, i was able to come up with the following code which will parse through all of the switches on the command line and end when there are no switches left. The big thing here is the delimiter. I used a : as the delimiter and a / as a switch identification. You can made the delimiter whatever you want, but you cant use space, comma, semicolon, equal sign, tab, or carriage return as these are natural DOS delimiters which will give you two input variables. Hope this helps. :checkparameters :: REM Grab the first variable supplied as a whole. Ex. /action:start set SWITCHPARSE=%1 :: REM Check to see if there are no more switches, if so goto end of :: parsing, prevents endless loop IF [%SWITCHPARSE%] == [] goto endswitchparsing :: REM Reset variables as clean up. Dont know if i need this anymore :: since i am checking for exit above. Oh well. set SWITCH= set VALUE= :: In the SWITCHPARSE variable, grab the two tokens separated :: by a : and assign the first to SWITCH and the second to VALUE for /F "tokens=1,2 delims=: " %%a IN ("%SWITCHPARSE%") DO SET SWITCH=%%a&set VALUE=%%b :: Check which action to perform based on the switch IF [%SWITCH%] == [/action] goto setaction IF [%SWITCH%] == [/servicename] goto setsysservicename IF [%SWITCH%] == [/servername] goto setservername IF [%SWITCH%] == [/username] goto setpassedusername IF [%SWITCH%] == [/userpassword] goto setpasseduserpasswd :: Perform the action by setting the variable for later use and :: shift the command line parameters so the next in line is :: ready to be processed :setaction set ACTION=%VALUE% SHIFT goto checkparameters :setsysservicename set SYSSERVICENAME=%VALUE% SHIFT goto checkparameters :setservername set SERVERNAME=%VALUE% SHIFT goto checkparameters :setpassedusername set PASSEDUSERNAME=%VALUE% SHIFT goto checkparameters :setpasseduserpassswd set PASSEDUSERPASSWD=%VALUE% SHIFT goto checkparameters :endswitchparsing |
|
#4
|
|||
|
|||
|
Hi sortix!
First - thanks for posting a solution. Quite by accident I discovered that the equal sign gets treated the same as a space (at least for my purposes), and I eventually found another solution based upon that. See how I solved here: Code:
|
|
#5
|
|||
|
|||
|
Well...what started this whole thing was that I wanted to create a batch file that allowed for parameters to be passed to it. This was pretty easy since all you have to do is pass the parameters and then use %1, %2, %3, etc to use them inside the script. After I started it, I thought why not make it so that the parameters didn’t have to be in a certain order. That is what got me looking into a way to parse the parameters so that the script could determine which parameter was what and assign the parameter value to a variable. The first thing I came across that helped me was your post, mainly your check for switches function and the associated variable assignment functions that utilized the SHIFT command. After reading about the shift command, I realized that I could use that to process each passed parameter until there were no parameters left, in which case you can go to endswitchparsing. I was using -parameter=value but it wasn’t giving me the results I was expecting. Mainly it was treating -parameter as one passed parameter and then value as the next and would repeat this pattern for any remaining variables. I had a hunch that the equals sign was some sort of a natural delimiter.
After a lot of Google searching, I found http://www.robvanderwoude.com/index.html which is a wonderful source of batch scripting knowledge. At Rob Vanderwoude's web site, if you click on Batch Files on the left and then click on Parameters you will find the information on natural delimiters. As it says space, comma, semicolon, equal sign, tab, and carriage return are natural delimiters for batch file command line parameters. This explained the behavior I was experiencing. I decided not to use a natural delimiter in case I decided to have a parameter passed with no value such as /debug. Now I had to figure out how to parse the whole value of /switch:value to two separate values of /switch and value. After looking at some code examples, I found the following code: for /f "tokens=2-4 delims=/ " %%a in ("%DATE%") do ( set dd=%%a set mm=%%b set yyyy=%%c) After running this code, I was intrigued by the results I obtained. The delimiter "/" was discarded and each of the three date values were assigned to the temporary FOR variables of %%a, %%b, and %%c which were then assigned to dd, mm, and yyyy. I found another web site which I can’t locate any longer that explained why this was so. The first %%a is explicit as you state it right in the command. When you use a delimiter and/or tokens, there are implicit variables that start alphabetically after the first explicit variable you use. So in the example above, there is a %%b and a %%c. If there were more results from the date command, then there would be a %%d, %%e, %%f...etc. If you explicitly used %%d in the FOR command, then the next implicit variable is %%e, %%f, %%g...etc. So in my code: for /F "tokens=1,2 delims=: " %%a IN ("%SWITCHPARSE%") DO SET SWITCH=%%a&set VALUE=%%b I used the ":" as my delimiter in the command line parameters because I knew that I could split the parameter into two pieces with this FOR statement. Then I used the SWITCH variable to determine which set function to goto and the VALUE variable to set a local variable to the value of the parameter passed. Hope that makes more sense. I took four Java programming classes in college a couple of years ago which helped me out a lot with the logic behind what I wanted to do. |
|
#6
|
|||
|
|||
|
Thanks for the link. No other way to describe that site but - WOW! I found today in my hundreds of poorly organized bookmarks that I had run across that site once before, but somehow it became lost in the dozens of sites vainly attempting to offer help with batch scripting. Until now "/?" has been of more help to me than most of what I found on the web. This one leaves hardly a stone unturned.
As with most programming tasks, there is usually more than one way to skin a cat. In Windows 2000 you can also parse strings using SET. Take a look at this page: DOS Batch File Fun: Creating a subdirectory named after today's date. Beyond fun, a more useful application of this might be to create files that document (within their names) the exact time of their creation, then they are automatically listed in order in Explorer and I don't have to look beyond the filename to know the date. Although intrigueing, I chose not to persue this approach to parsing command line arguments because it does not work in DOS v7 (my GHOSTRUN.BAT is intended to be run from a boot floppy), and it is inherintly more complex than the simple approach of testing the value of one parameter to decide what to do with the next. Parsing with FOR is almost as simple but also fails with older versions of DOS. I initially conceded to go with for example "/dest" followed by a space and the path like "V:\IMAGES\". Later I discovered by accident (after much wasted time searching the web) that the equal sign gets treated the same as a space. So now I have "/dest=V:\IMAGES\" which looks more DOSlike and less UNIXlike on the command line. I'm still frustrated trying to find explanations of what ALL the special characters (like "=") do on the command line. I see you used "&" to combine two commands on one line. Quote:
I spent a couple years working in VMS, among other things, writing batch programs (VMS Command procedures). Almost every OS command in VMS has a "/SHOW" switch you can use to provide a verbatum explanation of what it will do (without "/SHOW"). This lets you tweak the parameters until you have it right so you don't end up with a horrible mess because of a poor choice of parameters. Sadly, I haven't seen this feature with other operating systems. I try to incorporate this feature into my batch programs whenever possible. I also like to add a "/?" switch (echo the header remarks) and do some syntax checking and display some basic syntax rules if a problem is found. Thanks for explaining the FOR command. That's one I haven't used in a DOS batch file. I'm starting to see a lot of potential for complex string processing. |
![]() |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Rate This Thread | |
|
|