%~What does d0 mean in Windows batch files?

I am looking at the batch file that defines the following variables:

set _SCRIPT_DRIVE=%~d0
set _SCRIPT_PATH=%~p0
  • %~What does d0 or% ~ p0 mean?
  • Is there a well-known set of values for things like the current directory, drive, script parameters?
  • Can I use other similar shortcuts?

#1 building

from File name resolution and more idioms in batch file Real method :

Path of script (no drive): ~ p0

Script drive: ~ d0

#2 building

%~d0 provides you with the drive letter of parameter 0 (script name),% - p0 is the path.

#3 building

They are enhanced variable replacements. They modify% N variables used in the batch file. It's very useful if you do batch programming in Windows.

%~I         - expands %I removing any surrounding quotes ("")
%~fI        - expands %I to a fully qualified path name
%~dI        - expands %I to a drive letter only
%~pI        - expands %I to a path only
%~nI        - expands %I to a file name only
%~xI        - expands %I to a file extension only
%~sI        - expanded path contains short names only
%~aI        - expands %I to file attributes of file
%~tI        - expands %I to date/time of file
%~zI        - expands %I to size of file
%~$PATH:I   - searches the directories listed in the PATH
               environment variable and expands %I to the
               fully qualified name of the first one found.
               If the environment variable name is not
               defined or the file is not found by the
               search, then this modifier expands to the
               empty string

You can run the FOR /? Find the above FOR /? .

#4 building

The magic variable% n contains the parameters used to call the file:% 0 is the path to the bat file itself,% 1 is the first parameter after it,% 2 is the second, and so on.

Since parameters are usually file paths, there is some extra syntax to extract part of the path. ~d is the drive, ~ p is the path (no drive), ~ n is the filename. They can be used in combination ~ dp is the drive + path.

Therefore,% - dp0 is very useful in bat: it is the folder where bat files are executed.

You can also get other types of meta information about the file: ~ t is the timestamp, ~ z is the size.

Here View references for all command line commands. Description under the magic code of the brand name Of .

#5 building

Yes, you can use the other shortcuts given below. In your command, ~ d0 means the drive letter for parameter 0.

~ expands the given variable
d gets the drive letter only
0 is the argument you are referencing

Because the 0 parameter is the script path, it gets you the drive letter for the path. You can also use the following shortcuts.

%~1         - expands %1 removing any surrounding quotes (")
%~f1        - expands %1 to a fully qualified path name
%~d1        - expands %1 to a drive letter only
%~p1        - expands %1 to a path only
%~n1        - expands %1 to a file name only
%~x1        - expands %1 to a file extension only
%~s1        - expanded path contains short names only
%~a1        - expands %1 to file attributes
%~t1        - expands %1 to date/time of file
%~z1        - expands %1 to size of file
%~$PATH:1   - searches the directories listed in the PATH
               environment variable and expands %1 to the fully
               qualified name of the first one found.  If the
               environment variable name is not defined or the
               file is not found by the search, then this
               modifier expands to the empty string    

%~dp1       - expands %1 to a drive letter and path only
%~nx1       - expands %1 to a file name and extension only
%~dp$PATH:1 - searches the directories listed in the PATH
               environment variable for %1 and expands to the
               drive letter and path of the first one found.
%~ftza1     - expands %1 to a DIR like output line

When you run CALL /? You can also find it directly at the command prompt. Or FOR /?

Tags: Programming Windows

Posted on Mon, 09 Mar 2020 03:17:00 -0400 by karimali831