I'm looking at a batch file which defines the following variables:
set _SCRIPT_DRIVE=%~d0 set _SCRIPT_PATH=%~p0
%~d0
or %~p0
actually mean?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 %~dp0 (that's a zero) variable when referenced within a Windows batch file will expand to the drive letter and path of that batch file. The variables %0-%9 refer to the command line parameters of the batch file. %1-%9 refer to command line arguments after the batch file name. %0 refers to the batch file itself.
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.
Save the current directory on a stack and change to %~dp0 which is the drive-and-path of the "0'th" command-line parameter (which is the command itself) so the destination path to be set is the drive/path of the batch file to be executed.
The magic variables %
n contains the arguments used to invoke the file: %0
is the path to the bat-file itself, %1
is the first argument after, %2
is the second and so on.
Since the arguments are often file paths, there is some additional syntax to extract parts of the path. ~d
is drive, ~p
is the path (without drive), ~n
is the file name. They can be combined so ~dp
is drive+path.
%~dp0
is therefore pretty useful in a bat: it is the folder in which the executing bat file resides.
You can also get other kinds of meta info about the file: ~t
is the timestamp, ~z
is the size.
Look here for a reference for all command line commands. The tilde-magic codes are described under for.
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