Dos - For Statement

Card Puncher Data Processing

About

The for statement:

To get more information on this statement, just type in a command dos:

help for

Variable

Mode

The variable element initialization differs from one interaction mode to the other

  • %variable is used in on-line mode
  • %%variable is used in (batch|script) mode

Syntax Enhancement

To understand the syntax enhancement, you must understand the FOR loop constructs. This why they are at the end of this page: see variable_enhancement

Type For

File Set

Syntax

FOR [/R path] (% of %%)variable IN (set) DO command [command-parameters]

where:

  • the /R option indicates if you want to recursively walk the directory tree from the path parameter (default: current directory)
  • % and %% indicates that we are in presence of a variable
    • % is used in on-line mode
    • %% is used in batch mode
  • variable is a single letter variable
  • set is a file set data definition (not the set function of variable). You can use the ster wildcard and use a set definiton. To get all doc and txt files, you will define (set) as:
(*.doc *.txt )

  • command-parameters: specifies parameters or switches for the specified command.

Example

List all files in one directory
:: In the command line
for %v in (*.bat) do echo %v
:: In a DOS Script
for %%v in (*.bat) do echo %%v

where:

  • v is the variable
  • *.bat is the set of all file with a bat extions file

Example of output:

start.bat
Hello.bat

List all files in the directory tree

Current directory = C:\Users\gerard

:: In the command line
for /R %v in (*.bat) do echo %v
:: In a DOS Script
for /R %%v in (*.bat) do echo %%v
:: Is the same that
for /R C:\Users\gerard %%v in (*.bat) do echo %%v
C:\Users\gerard\AppData\Local\Temp\cleanup_bootstrap.bat
C:\Users\gerard\AppData\Local\Temp\env.bat
C:\Users\gerard\AppData\Local\Temp\startup.bat
C:\Users\gerard\AppData\Local\Temp\update_acls1.bat
C:\Users\gerard\AppData\Local\Temp\update_acls2.bat
..........

Directory Set

Syntax

FOR /D (%|%%)variable IN (set) DO command [command-parameters]

where:

  • % and indicates that we are in presence of a variable * % is used in on-line mode * is used in batch mode
  • variable is a single letter variable
  • set is a directory set data definition (not the set function of variable). You can use the star wildcard and use a set definition. To get all directory which start with Doc and , you will define (set) as:
(App* Pic*)

  • command-parameters: specifies parameters or switches for the specified command.

Example

:: In the command line
FOR /D %v IN (App* Pic*) DO echo %v
:: In a DOS Script
FOR /D %%v IN (App* Pic*) DO echo %%v

where:

  • v is the variable
  • (App* Pic*) is the set of directory that start with App and Pic

Example of output from the User Home:

AppData
Pictures

Sequence of number

Syntax

FOR /L (%|%%)variable IN (start,step,end) DO command [command-parameters]

where:

  • /L indicate a number for loop
  • start indicates the first number of the sequence
  • end indicates the last number of the sequence
  • step indicate the number of number to jump

Example

The set is a sequence of numbers from start to end, by step amount.

FOR /L %v IN (3,3,15) DO echo %v

Output:

3
6
9
12
15

(File|Text|Command Output) Parsing

for parsing with the /F switch, see DOS - Parsing (File, Command, Variable) - FOR F option

Variable Enhancement

Syntax

%~mI

where:

  • %~
  • m is a modifier (They are not case sensitive)
  • I is valid FOR variable

Picking upper case variable names like %I makes it more readable and avoids confusion with the modifiers, which are not case sensitive.

List of modifiers

List of syntax enhancement with example:

In the below examples %I can be replaced by other valid values.

No Modifiers: removing any surrounding quotes

%~I expands %I removing any surrounding quotes (“)

f: fully qualified path name

%~fI expands %I to a fully qualified path name

for %I in (.) do @echo %~fI
c:\Users\gerard

d: Drive letter only

%~dI expands %I to a drive letter only

for %I in (.) do @echo %~dI
c:

p: Path only

%~pI - expands %I to a path only

for %I in (.) do @echo %~pI
\Users\

n: file name only

%~nI expands %I to a file name only

for %I in (.) do @echo %~nI
gerard

x: file extension only

%~xI expands %I to a file extension only

for %I in (temp.xml) do @echo %~xI
.xml

s: short names only

%~sI expanded path contains short names only. Example from C:\Program Files

for %I in (.) do @echo %~sI
C:\PROGRA~1

a: file attributes of file

%~aI expands %I to file attributes of file

for %I in (.) do @echo %~aI
d--------

t: date/time of file

%~tI expands %I to date/time of file

for %I in (.) do @echo %~tI
2013-10-08 12:01

z: size of file

%~zI expands %I to size of file

for %I in (.) do @echo %~tI
28672

PATH: Search file in directories

%~PATH:I searches the directories listed in the PATH environment variable and expands %I to the fully qualified name of the first one found. If the environment variable name is not defined or the file is not found by the search, then this modifier expands to the empty string. Example: Where can I find SQLPLUS.exe

for %I in (sqlplus.exe) do @echo %~$PATH:I
C:\app\gerard\product\12.1.0\dbhome_1\BIN\sqlplus.exe

PATH can be replaced by other valid values.

Modifiers Combination

The modifiers can be combined to get compound results:

  • %~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
  • %~dpPATH:I - searches the directories 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 a DIR like output line

Support

%% was unexpected at this time

See variable





Discover More
Card Puncher Data Processing
DOS - Delayed environment variable

From the set help. The current expansion happens when a text block is read, not when it is executed. Here below, you have the example of an immediate variable expansion with an if block The following...
Card Puncher Data Processing
DOS - File

File in DOS. See ... in a for loop: the file name without extension. Fully Qualified Path Name See if exist statement See: ERASE Deletes one or more files. type...
Card Puncher Data Processing
DOS - Parse a property file

How to parse a property file in DOS with the F option of the FOR loop. Dos Script to list the content Conditional processing with a if statement:
Card Puncher Data Processing
DOS - Parsing (File, Command, Variable) - FOR F option

parsing in DOS is done via the F option of the FOR command where: the option /F means (file|text) processing % and %% indicates that we are in presence of a variable % is used in on-line mode...
Card Puncher Data Processing
Dos - (Batch) Script

The batch script is a text file with the extension .bat or .cmd that is interpreted by a batch interpreter. You can call a batch script by : writing its name in the source script: The script execution...
Card Puncher Data Processing
Dos - (Environment) Variable

How to manage variable in dos. A variable name: must only be made up of: alphabetic characters, numeric characters and underscores, cannot begin with a numeric character. cannot be...
Card Puncher Data Processing
Dos - Command

A command is: a Dos command (See below). of an utility. The DOS command are extended with management tools located in C:\Windows\System32...
Card Puncher Data Processing
Dos - Control Flow

The flow in DOS can be controlled via: Labels that can be called with the goto (inside the script) or call (outside the script) FOR runs a specified command for each file in a set of files
Card Puncher Data Processing
How to resize images in a directory with ffmpeg ?

This article shows you how to scale image in a dirctory with ffmpeg and conserve the aspect ratio. Because ffmpeg is a video management tool, it can also modify a frame, in other word, an image. ...
Java Conceptuel Diagram
Java - (Jar|Java ARchive) File

JAR stands for Java ARchive and is file format born in 1996 as a simple extension of the popular ZIP archive format with class and other required resource files: manifest signature files images,...



Share this page:
Follow us:
Task Runner