Batch FOR / DO

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
Kircata
Posts: 2
Joined: 29 Apr 2017 07:57

Batch FOR / DO

#1 Post by Kircata » 29 Apr 2017 08:02

Hello people!
I have interest in learning batch and I'm having a struggle understanding the commands FOR and DO. If any of you could explain me in more detail what do they do it would be really appreciated :)

aGerman
Expert
Posts: 4654
Joined: 22 Jan 2010 18:01
Location: Germany

Re: Batch FOR / DO

#2 Post by aGerman » 29 Apr 2017 11:22

Well DosTips is not an internet tutorial. It's rather a discussion forum. So you should explain what exactly you want to achieve in order to get help for a certain problem. A FOR loop is not the same as a FOR loop. There are different kinds such as FOR /D, FOR /R, /FOR /L, FOR /F. Meanwhile you might want to read the reference ...
http://www.dostips.com/DosCommandIndex.php#FOR

E.g. on SS64 you'll find extended informations for every kind of FOR loop beginning there:
https://ss64.com/nt/for.html

Steffen

dbenham
Expert
Posts: 2461
Joined: 12 Feb 2011 21:02
Location: United States (east coast)

Re: Batch FOR / DO

#3 Post by dbenham » 29 Apr 2017 12:16

The FOR command is the most complicated batch command because it does so many different things, depending on the syntax that you use.

You asked to describe FOR and DO, but really you need an explanation of three things: FOR ... IN(...) DO ...

What makes the command so complicated is there are so many radically different things that the construct can do, depending on what options you use. The one thing that all forms have in common is they all iterate a collection of things.

The FOR ... section defines (or partially defines) what type of collection you want to iterate, and exactly what rules you want to use

The IN(...) section defines the actual group of things that will be iterated, and can also assist in specifying the type of things to iterate

The DO ... section specifies exactly what you want to do to each thing (what to do with each iteration)

I will attempt to outline all the possibilities but it probably won't make much sense until you see working examples.


1) Iterate a list of strings (often times the string represents a file, but it does not have to)

Code: Select all

FOR %%A IN (string1 string2 .... stringN) DO ...


2) Iterate files based on wild cards. A folder path may exist, without wild cards, and the file name portion must contain at least one wild card (* or ?). The command will iterate all files that match the file mask at the specified path. The default path is the current directory.

Code: Select all

FOR %%A IN (optionalPath\file*.txt) DO ...


3) Iterate folders based on wild cards with the /D option. The command will iterate all folders that match the folder mask at the specified path. The default path is the current directory. The /D option has no impact unless there is at least one wildcard. Using the /D option without a wildcard functions exactly the same as form 1)

Code: Select all

FOR /D %%A IN (optionalPath\folder*name) DO ...


4) Do any of the first 3 things in the context of a folder hierarchy using the /R (recurse) option. Note that the IN clause cannot include a path if you use /R. Instead, the root path must follow the /R option. The default root is the current directory.

4-1) Recursively iterate all folders within a folder hierarchy, and for each folder iteration, iterate each of the IN() strings

Code: Select all

FOR /R optionalRootPath %%A IN (string1 string2 ... stringN) DO ...

4-2) Recursively iterate all files that match the IN file mask within the folder hierarchy

Code: Select all

FOR /R optionalRootPath %%A IN (file*mask1  file*mask2 .... )  DO ...

4-3) Recursively iterate all folders that match the IN folder mask within the folder hierarchy by using both /D and /R

Code: Select all

FOR /D /R optionalRootPath %%A IN (folder*mask1  folder*mask2 ... ) DO ...


5) Iterate lines using the /F option. Each line is parsed into 1 or more tokens. The source of the lines is determined by the syntax used in the IN() clause

The /F option has various sub-options that can be specified after /F. The /F options are usually specified as a single quoted string

DELIMS specifies what characters are used to delimit tokens (defaults to <space> and <tab>)
TOKENS specifies which tokens within each line should be parsed and assigned to a token variable (defaults to the first token)
SKIP specifies how many lines to skip at the beginning (defaults to 0)
EOL specifies which character specifies that a line should be skipped. If a line begins with the EOL character, then the line is skipped (defaults to a semicolon)
USEBACKQ specifies an alternate syntax to use in the IN() clause

5-1) Iterate the lines in one or more files. Wildcards cannot be used. The IN clause must not be quoted, or else double quotes are used with the USEBACKQ option

Code: Select all

FOR /F "options" %%A IN (file1  file2 ...) DO ...

FOR /F "USEBACKQ otherOptions" %%A in ("file 1" "file 2") DO
- note this is the only case where USEBACKQ is really needed, when you want to read a file and the file name must be quoted because it contains spaces.

5-2) Iterate and parse all lines within double quoted string literal (or a single quoted string literal if USEBACKQ is used)

Code: Select all

FOR /F "options" %%A IN ("string") DO ...

FOR /F "USEBACKQ otherOptions" %%A in ('string') DO ...

5-3) Iterate and parse all lines of output from a command (only lines output to stdout) The command must be enclosed in single quotes (or backticks if USEBACKQ is used)

Code: Select all

FOR /F "options" %%A IN ('command') DO ...

FOR /F "USEBACKQ otherOptions" %%A IN ('someCommand') DO ...


Dave Benham

Kircata
Posts: 2
Joined: 29 Apr 2017 07:57

Re: Batch FOR / DO

#4 Post by Kircata » 29 Apr 2017 13:32

Thanks for the quick response! I really appreciate it! I really find it difficult to understand but this answer helped me a bit. Maybe next time when I have a struggle with some batch program I will write again. Once again thank you fot that response. I'm going to save that text you wrote so it could help me. I really didn't think anyone would answer me but as I see the community here is really friendly :)

Post Reply