FOR /F
Loop command: against a set of files - conditionally perform a command against each item.
Syntax
FOR
/F ["
options"] %%
parameter IN (
filenameset) DO
command
FOR
/F ["
options"] %%
parameter IN ("Text string to process") DO
command
Key
options:
delims=
xxx The delimiter character(s) (default = a space)
skip=
n A number of lines to skip at the beginning of the
file.
(default = 0)
eol=; Character at the start
of each line to indicate a
comment
The
default is a semicolon ;
tokens=
n Specifies which numbered items to read from each
line
(default = 1)
usebackq Use the alternate quoting style:
- Use double quotes
for long file names in
"
filenameset".
- Use single quotes for 'Text string to process'
- Use back quotes for `
command to process
`
Filenameset A set of one or more files. Wildcards may be used.
If (filenameset) is a period character (.) then FOR
will
loop through every file in the folder.
command The command to carry out,
including any
command-line parameters.
%%
parameter A replaceable parameter:
in a batch file use %%G (on the command line %G)
FOR /F processing of a text file consists of reading the file, one line of text at a time and then
breaking the line up into individual items of data called 'tokens'. The DO command is then
executed with the parameter(s) set to the token(s) found.
By default, /F breaks up the line at each blank space
" "
, and any blank lines are skipped, this
default parsing behavior can be changed by
applying one or more of
the
"
options"
parameters. The option(s) must be contained within "a pair of quotes"
Within a FOR loop the visibility of FOR variables is controlled via
SETLOCAL
EnableDelayedExpansion
“It's completely intuitive; it just takes a few days to learn, but then it's completely intuitive” -
Terry Pratchett.
FOR /F
Loop command: against the results of another command.
Syntax
FOR
/F ["
options"] %%
parameter IN (
'command_to_process') DO
command
Key
options:
delims=
xxx The delimiter character(s)
(default = a space)
skip=
n A number of lines to skip at the beginning.
(default = 0)
eol=; Character at the start of each line to indicate a
comment
The default is a semicolon ;
tokens=
n The numbered items to read from each line
(default = 1)
usebackq Use the alternate quoting style:
- Use double quotes for long file names in
"
filenameset".
- Use single quotes for 'Text string to process'
- Use back quotes for `command_to_process`
command_to_process : The output of the 'command_to_process' is
passed into the FOR parameter.
command : The command to carry out, including any
command-line parameters.
%%
parameter : A replaceable parameter:
in a batch file use
%%G (on the command line
%G)
FOR /F processing of a command consists of reading the output from the command one line at a
time and then breaking the line up into individual items of data or 'tokens'. The DO command is
then executed with the parameter(s) set to the token(s) found.
The FOR command is the answer to innumerable questions where you want to take the output
of some command, store it in a variable (%%G) then do something with the result.
For example the PING command returns serveral lines including one like:
Packets: Sent = 4, Received = 4, Lost = 0 (0% loss),
To select that one line of output, you can search for the text "loss" (which is always present),
then use the Tokens parameter to select the number of lost packets, here this is
0
but it will vary
each time you run the command.
set _ping_cmd=ping -n 5 127.0.0.1
FOR /f "tokens=4 delims=(=" %%G IN ('%_ping_cmd% ^|find "loss"') DO
echo Result is [%%G]
The tricky part is always splitting up the line of interest into the right tokens, in this case I'm
splitting on the characters '=' and '('
these two characters split the line into 5 chunks of text and we pull out the fourth one with
"tokens=4"
By default, /F breaks up the command output at each blank space, and any
blank lines are
skipped.
You can override this default parsing behavior by specifying the "options" parameter. The
options must be contained within "quotes"
Chia sẻ với bạn bè của bạn: