Using the FOR Command in Windows (with examples)
- Windows
- November 5, 2023
The FOR command in Windows is a powerful tool for executing commands repeatedly based on specific conditions. It allows you to iterate through lists of items, perform actions on them, and customize the behavior using different options. In this article, we will explore five different use cases of the FOR command and provide code examples for each.
1: Executing commands for a specified set of items
The first use case is to execute a set of commands for a specified list of items. This can be useful when you want to perform the same action on multiple items. Here is an example:
for %variable in (item_a item_b item_c) do (
echo Loop is executed
)
In this example, the FOR command will iterate through the items “item_a”, “item_b”, and “item_c” and execute the command within the parentheses for each item. The output will be:
Loop is executed
Loop is executed
Loop is executed
This use case is helpful when you need to perform repetitive tasks on a specific set of items.
2: Iterating over a range of numbers
The second use case is to iterate over a range of numbers and perform actions on them. This can be useful when you want to repeat a command a certain number of times or perform calculations. Here is an example:
for /l %variable in (1, 1, 5) do (
echo Loop is executed
)
In this example, the FOR command will iterate through the numbers 1 to 5 with a step size of 1 and execute the command within the parentheses for each number. The output will be:
Loop is executed
Loop is executed
Loop is executed
Loop is executed
Loop is executed
This use case is handy for automating tasks that require repetitive calculations or operations.
3: Iterating over a list of files
The third use case is to iterate over a list of files and perform actions on them. This can be useful when you want to process multiple files using the same command. Here is an example:
for %variable in (path\to\file1.ext path\to\file2.ext ...) do (
echo Loop is executed
)
In this example, the FOR command will iterate through the given paths of files and execute the command within the parentheses for each file. The output will be:
Loop is executed
Loop is executed
This use case is valuable when you need to perform batch operations on multiple files.
4: Iterating over a list of directories
The fourth use case is to iterate over a list of directories and perform actions on them. This can be useful when you want to process multiple directories using the same command. Here is an example:
for /d %variable in (path\to\directory1.ext path\to\directory2.ext ...) do (
echo Loop is executed
)
In this example, the FOR command will iterate through the given paths of directories and execute the command within the parentheses for each directory. The output will be:
Loop is executed
Loop is executed
This use case is beneficial for performing repetitive tasks on multiple directories.
5: Performing a command in every directory
The fifth use case is to perform a specific command in every directory. This can be useful when you want to apply a command to all directories within a specific location. Here is an example:
for /d %variable in (*) do (
if exist %variable echo Loop is executed
)
In this example, the FOR command will iterate through all directories in the current location and execute the command within the parentheses for each directory if it exists. The output will be:
Loop is executed
This use case is handy for executing commands on multiple directories without having to specify each directory explicitly.
In this article, we have explored five different use cases of the FOR command in Windows. We have provided code examples along with motivations, explanations for each argument, and example outputs. The versatility of the FOR command allows for automation and batch operations, making it a valuable tool for managing repetitive tasks in the Windows command prompt.