text
stringlengths
0
4.23k
Expands path to contain short names only.
%~aI
Expands %I to the file attributes of file.
%~tI
Expands %I to the date and time of file.
%~zI
Expands %I to the size of the file.
%~$PATH:I
Searches the directories listed in the PATH environment variable and expands %I to the fully qualified name of the first directory found. If the environment variable name is not defined or the file is not found by the search, this modifier expands to the empty string.
%~I
%I
%~fI
%I
%~dI
%I
%~pI
%I
%~nI
%I
%~xI
%I
%~sI
%~aI
%I
%~tI
%I
%~zI
%I
%~$PATH:I
%I
The following table lists modifier combinations that you can use to get compound results.
Variable with combined modifiers
Description
%~dpI
Expands %I to a drive letter and path only.
%~nxI
Expands %I to a file name and extension only.
%~fsI
Expands %I to a full path name with short names only.
%~dp$PATH:I
Searches the directories that are listed in the PATH environment variable for %I and expands to the drive letter and path of the first one found.
%~ftzaI
Expands %I to an output line that is like dir.
%~dpI
%I
%~nxI
%I
%~fsI
%I
%~dp$PATH:I
%I
%~ftzaI
%I
In the above examples, you can replace %I and PATH with other valid values. A valid for variable name ends the %~ syntax.
%I
By using uppercase variable names such as %I, you can make your code more readable and avoid confusion with the modifiers, which are not case sensitive.
%I
Parsing a string: You can use the for /f parsing logic on an immediate string by wrapping <literalstring> in either: double quotes (without usebackq) or in single quotes (with usebackq) --for example, (MyString) or ('MyString'). <literalstring> is treated as a single line of input from a file. When parsing <literalstring> in double-quotes, command symbols such as (\ & | > < ^) are treated as ordinary characters.
for /f
<literalstring>
<literalstring>
<literalstring>
\ & | > < ^
Parsing output: You can use the for /f command to parse the output of a command by placing a back-quoted <command> between the parentheses. It is treated as a command line, which is passed to a child Cmd.exe. The output is captured into memory and parsed as if it is a file.
for /f
<command>
Examples
To use for in a batch file, use the following syntax:
for {%%|%}<variable> in (<set>) do <command> [<commandlineoptions>]
To display the contents of all the files in the current directory that have the extension .doc or .txt by using the replaceable variable %f, type:
for %f in (*.doc *.txt) do type %f
In the preceding example, each file that has the .doc or .txt extension in the current directory is substituted for the %f variable until the contents of every file are displayed. To use this command in a batch file, replace every occurrence of %f with %%f. Otherwise, the variable is ignored and an error message is displayed.
To parse a file, ignoring commented lines, type:
for /f eol=; tokens=2,3* delims=, %i in (myfile.txt) do @echo %i %j %k
This command parses each line in myfile.txt. It ignores lines that begin with a semicolon and passes the second and third token from each line to the for body (tokens are delimited by commas or spaces). The body of the for statement references %i to get the second token, %j to get the third token, and %k to get all of the remaining tokens. If the file names that you supply contain spaces, use quotation marks around the text (for example, File Name). To use quotation marks, you must use usebackq. Otherwise, the quotation marks are interpreted as defining a literal string to parse.
%i is explicitly declared in the for statement. %j and %k are implicitly declared by using tokens=. You can use tokens= to specify up to 26 tokens, provided that it does not cause an attempt to declare a variable higher than the letter z or Z.
To parse the output of a command by placing set between the parentheses, type:
for /f "usebackq delims==" %i in (`set`) do @echo %i