|
Answer» So forgive my ignorance, but I can't seem to find out what these actually do. Google ignores the percent sign (%), and I'm not sure what they're called. Would someone mind explaining them?
Thanks.They are called "batch parameters". %1 to %9 are "replaceable parameters". As you may guess, you can have up to NINE of them. They are passed to a batch from the command line or another batch, you put them after the batch name. They are optional. Parameters are most often separated by spaces, but any of the following are also valid delimiters: COMMA, semicolon, equals sign, tab.
here is a batch called test.bat
Code: [Select]@echo off echo first parameter %1 echo second parameter %2 echo third parameter %3
if you are at a PROMPT in the same folder (or you type the full path to the batch) and you type test.bat cat dog horse
you will see this
Code: [Select]@echo off echo first parameter cat echo second parameter dog echo third parameter horse
Try this for yourself. Can you guess what happens if you type test.bat "cat dog horse"?
You can use a tilde to strip quotes, if there are any, like this %~1
%0 is a special parameter, it is the command used to start the batch (i.e. its name).
The above should give you some ideas to play with.
Also study the SHIFT command.
Note that %* means ALL the parameters that were passed including delimiters (joined into one string)
Cool. Thanks for clearing it up I was feeling REALLY stupid reading other people's code and running across them.
|