How to Use Batch File For Loop in Windows

How to Use Batch File For Loop to Process Files and Folders?

One of the common uses of batch file for loops is to process files and folders in bulk. For example, you may want to rename, delete, or move a number of files or folders in a specific pattern. To do this, you can use some of the additional options available for for loops, such as:

  • /F to process text files or results from other commands.
  • /R to process subfolders recursively.
  • /D to process directories only.

Here is the syntax of the for loop batch file with these additional options:

FOR /F ["options"] %%parameter IN (set) DO command

Where:

  • [“options”] are additional options that can be used to control how the for loop processes text files or command results. Some commonly used options are:
FOR /R [[drive:]path] %%parameter IN (set) DO command

Where:

  • [[drive:]path] is the starting directory to be processed by the loop. If not specified, the current directory will be used.
  • (set) is the set of files to be processed by the loop. This set must be a file matching pattern (file globbing), such as  *.txt  or  ?.bat. If this set is empty, then all files will be processed.
FOR /D %%parameter IN (set) DO command

Where:

  • (set) is the set of directories to be processed by the loop. This set must be a file matching pattern (file globbing), such as  * or ?. If this set is empty, then all directories will be processed.

Example:

FOR /F "skip=1 delims=," %%A IN (data.csv) DO ECHO %%A

The above command will skip the first line of the data.csv file, which usually contains the column headings, and display the first column value of each subsequent line, separated by commas.

FOR /F "tokens=1,2* usebackq" %%A IN ("file name.txt") DO ECHO %%A %%B %%C

The above command will process a text file whose name contains spaces, and display the first, second, and remaining column values of each line, separated by spaces or tabs.

FOR /R C:\Users\ %%D IN (*.jpg) DO COPY %%D D:\Backup\

The above command will search for all files with the .jpg extension in the C:\Users\ directory and its subdirectories, and copy them to the D:\Backup directory.

FOR /D %%E IN (*) DO RMDIR /S /Q %%E

The above command will delete all directories and their contents in the current directory.

How to Use Batch File For Loop to Access File and Folder Information?

In addition to processing files and folders with the batch file for loop, you can also access information about them, such as name, extension, size, modification date, attributes, and path.

To do this, you can use path modifiers on loop variables. A path modifier is a special character that can be added after the percent sign (%) in a loop variable to change the value of that variable.

Here are some commonly used path modifications:

  • %~fparameter to display the full path of a file or folder.
  • %~nparameter` to display file or folder names without extensions.
  • %~xparameter to display file or folder extensions.
  • %~sparameter to display the short path of a file or folder, which is useful if the path contains spaces.
  • %~zparameter to display the file or folder size in bytes.
  • %~tparameter to display the date and time of file or folder modification.
  • %~aparameter to display file or folder attributes, such as R (read-only), H (hidden), S (system), etc.

Example:

FOR %%F IN (*.txt) DO ECHO %%F %%~nF %%~xF %%~sF %%~zF %%~tF %%~aF

The above command will display information about all text files in the current directory, such as name, extension, short path, size, modification date, and attributes.

Latest Articles