I have seen the usage of %*
in batch files and command lines.
Can someone explain the typical usage of %*
with an example?
%%a refers to the name of the variable your for loop will write to. Quoted from for /? : FOR %variable IN (set) DO command [command-parameters] %variable Specifies a single letter replaceable parameter. (set) Specifies a set of one or more files. Wildcards may be used.
The tilde (~) sign is used in different ways in batch files: Argument quote removal. A tilde sign before an command-line argument (such as "%~1") indicates to remove the surrounding quotes from the parameter. Such if the value for %1 is "Hi" then %~1 will expand to only Hi.
The @ symbol tells the command processor to be less verbose; to only show the output of the command without showing it being executed or any prompts associated with the execution. When used it is prepended to the beginning of the command, it is not necessary to leave a space between the "@" and the command.
%f is a "for-variable" or "loop-variable" (I also heard the term "Metavariable"). It's valid only within the for loop and has no meaning after the for loop ends. Environment variables are manipulated with the set command (see set /? ), for for-variables, there are "modifiers" (see for /? ) – Stephan.
It means "all the parameters in the command line".
For example, it's useful when you want to forward the command line from your batch file to another program:
REM mybatchfile.cmd
echo You called this with arguments: %*
echo I will now forward these to the DIR command.
dir %*
One important point not listed in any of the previous answers: %*
expands to all parameters from the command line, even after a SHIFT
operation.
Normally a SHIFT
will move parameter %2
to %1
, %3
to %2
, etc., and %1
is no longer available. But %*
ignores any SHIFT
, so the complete parameter list is always available. This can be both a blessing and a curse.
%*
expands to the complete list of arguments passed to the script.
You typically use it when you want to call some other program or script and pass the same arguments that were passed to your script.
The
%*
modifier is a unique modifier that represents all arguments passed in a batch file. You cannot use this modifier in combination with the%~
modifier. The%~
syntax must be terminated by a valid argument value.
Source:
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With