/* steve jansen */

// another day in paradise hacking code and more

Windows Batch Scripting: Loops

| Comments

Looping through items in a collection is a frequent task for scripts. It could be looping through files in a directory, or reading a text file one line at a time.

Old School with GOTO

The old-school way of looping on early versions of DOS was to use labels and GOTO statements. This isn’t used much anymore, though it’s useful for looping through command line arguments.

:args
SET arg=%~1
ECHO %arg%
SHIFT
GOTO :args

New School with FOR

The modern way to loop through files or text uses the FOR command. In my opinion, FOR is the single most powerful command in DOS, and one of the least used.

GOTCHA: The FOR command uses a special variable syntax of % followed by a single letter, like %I. This syntax is slightly different when FOR is used in a batch file, as it needs an extra percent symbol, or %%I. This is a very common source of errors when writing scripts. Should your for loop exit with invalid syntax, be sure to check that you have the %% style variables.

Looping Through Files

FOR %I IN (%USERPROFILE%\*) DO @ECHO %I

Looping Through Directories

FOR /D %I IN (%USERPROFILE%\*) DO @ECHO %I

Recursively loop through files in all subfolders of the %TEMP% folder

FOR /R "%TEMP%" %I IN (*) DO @ECHO %I

Recursively loop through all subfolders in the %TEMP% folder

FOR /R "%TEMP%" /D %I IN (*) DO @ECHO %I

Variable Substitution for Filenames

<< Part 5 – If/Then Conditionals Part 7 – Functions >>

Comments