FOR

RECENT NEWS

12/18/2015 - Hello! We're excited to announce that Mental Works Computing Software will soon be featuring

12/11/2015 - We're going to announce Dr.Batcher 3.0 Alpha with batch files debugger support soon. If you want to start...

News history...

TESTIMONIALS

Rob van der Woude:
"Mental Works Computing Software has created a nice tool for...

Andrew Jaritsyn:
“I used to write batch files with the help of Notepad, but...

Steve H.:
“This tool helped me to write some batch files in a couple...

More...

FOR

Runs a command in a loop (several times).

Syntax:

FOR [/d] [/r] [[drive:]path] [/l] [/f] [“options”] %%|%Variable in (Set) do Command [CommandOptions]

/d – makes Set match directory names instead of file names.

/r [[drive:]path] – recursive: makes FOR command walk through the directory tree at the specified drive:path and perform Command for each file in each directory. If no directory specified, performs this for the current folder.

/l – makes FOR command iterate the set of numbers. Set should look like (Start, Step, End), where Start, Step, End are numbers. FOR command will iterate from Start to End adding Step to the Start until End is reached.

/f “options” – makes FOR command iterate the contents of file(s) line-by-line. Each line can be parsed into zero or more tokens. By default, FOR command /F passes first blank separated token from each line of each file, but it can be overridden with options:

eol=c – specifies single end-of-line character;

skip=N – specifies the number of lines (N) to skip at the beginning of file;

delims=XXX – specifies delimiters (XXX) instead of space and tab;

tokens=X,Y,M-N – specifies tokens for each line to be passed for each iteration (M-N specifies the range);

usebackq – specifies the execution of back-quoted string as a command and a single-quoted string as a literal string command.

Variable – replaceable parameter.

Set – group of files separated with spaces. Each file per iteration replaces the variable. Wildcards (*, ?) are allowed.

Command – command to be executed for each iteration.

CommandOptions – options for command to be executed.

In, do – necessary keywords.

Usage:

You can specify one option (/r, /l, /d, /f) per time.

You can use variable expansion masks:

%~I ‑ Expands %I which removes any surrounding quotation marks (” “).

%~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 name extension only.

%~sI ‑ 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 variable is not defined or the file is not found, this the result of expansion empty string.

You can use FOR /f to parse the output of another command.

If the last character in the tokens= string is *, an additional variable is allocated, and it receives the remaining text on the line after the last token that is parsed.

You can run multiple commands in FOR loop if you surround them with the brackets.

Examples:

Display the contents of each file in the current folder:

FOR %FL in (*.*) do TYPE %FL

Parsing tokens:

FOR /f “tokens=1-3” %%i in (“A B C D E F”) do MD c:\Test\%%G

 

Subscribe batch files programming newsletter (no spam):

Subscribe

Unsubscribe