For - Loop Through Command Output - Windows CMD
For - Loop Through Command Output - Windows CMD
html
ss64.com
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)
1 de 8 26/3/19 18:43
For - Loop through command output - Windows CMD about:reader?url=https://ss64.com/nt/for_cmd.html
2 de 8 26/3/19 18:43
For - Loop through command output - Windows CMD about:reader?url=https://ss64.com/nt/for_cmd.html
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.
usebackq
3 de 8 26/3/19 18:43
For - Loop through command output - Windows CMD about:reader?url=https://ss64.com/nt/for_cmd.html
Skip
eol
Often you will want to turn this feature off so that every line of
your data file is processed, in theory "eol=" should turn this
feature off, but in practice this fails to work correctly - it will
set eol to whatever the next character is, often the quote or
space character. One workaround is to set eol to some
unusual character that you don’t expect to ever encounter in
the data file e.g. "eol=€" or "eol=¬". Another method is to
escape every delimiter For /f tokens^=*^ delims^=^ eol^=
%%a in (file.txt) do... (see forum for a discussion of this)
Delims
4 de 8 26/3/19 18:43
For - Loop through command output - Windows CMD about:reader?url=https://ss64.com/nt/for_cmd.html
You can use any character as a delimiter, but they are case
sensitive.
If you don’t specify delims it will default to "delims=
<tab><space>"
n.b. some text editors will enter the TAB character as a series
of spaces, specifying more than one delimiter has been
known to cause problems with some data sets.
Tokens
tokens=2-6 will cause the second, third, fourth, fifth and sixth
items on each line to be processed
tokens=3* will process the third token and the 4th + all
subsequent items, this can also be written as tokens=3,*
5 de 8 26/3/19 18:43
For - Loop through command output - Windows CMD about:reader?url=https://ss64.com/nt/for_cmd.html
Examples:
6 de 8 26/3/19 18:43
For - Loop through command output - Windows CMD about:reader?url=https://ss64.com/nt/for_cmd.html
@echo off
::parse the VER command
FOR /F "tokens=4*" %%G IN ('ver') DO SET
_version=%%G
:: show the result
echo %_version%
The "tokens=*" has been added to match all parts of any long
filenames returned by the DIR command.
List all the text files in a folder, including the full path:
7 de 8 26/3/19 18:43
For - Loop through command output - Windows CMD about:reader?url=https://ss64.com/nt/for_cmd.html
Related:
8 de 8 26/3/19 18:43