Typically, I would have to execute a command for each folder separately as shown below:
and so on.
Now, I would like to create a for loop that runs the command for each folder and adds the directory, similar to the previous commands. However, I don’t want the loop to go through sub-folders. Since the folders only contain data, there is no need to exclude any folders or files. I am looking for a simple for loop that uses the Windows CMD (not PowerShell).
Can you please guide me on how to achieve this?
Looping Over a Directory’s Folders Using Windows CMD
When working with a large number of folders in Windows, it can be tedious and time-consuming to execute a command for each folder separately. Fortunately, Windows CMD provides a simple way to loop over a directory’s folders and perform a command on each one.
To achieve this, we can use the
FOR command in combination with the
/D switch, which specifies that we want to loop over directories (folders) only. We can also use the
%% variable to represent each folder name in the loop.
Here’s an example syntax for looping over a directory’s folders using Windows CMD:
FOR /D %f IN (C:data*) DO processdata.cmd %f
In the above command,
%f represents each folder name in the loop, and
processdata.cmd is the command that we want to execute for each folder. The
* wildcard character specifies that we want to loop over all folders in the
Let’s break down the command and see what each part does:
FOR is the command that we use to create a loop.
/D is the switch that tells the
FOR command to loop over directories only.
%f is the variable that we use to represent each folder name in the loop. We can use any valid variable name here.
IN (C:data*) specifies the directory that we want to loop over. The
* wildcard character matches any folder name in the directory.
DO processdata.cmd %f is the command that we want to execute for each folder.
%f is replaced with the current folder name in each iteration of the loop.
Note that we use a single
% when running the command directly from the command prompt, but we need to use double
%% when running the command from a batch file.
Excluding Subfolders from the Loop
In some cases, we may want to exclude subfolders from the loop and only process the top-level folders in a directory. To achieve this, we can add an additional switch to the
Here’s an example syntax for excluding subfolders from the loop:
FOR /D %f IN (C:data*) DO IF NOT EXIST %f* processdata.cmd %f
In the above command, we use the
IF NOT EXIST statement to check if the current folder (
%f) contains any files or subfolders. If the folder is empty, we execute the
processdata.cmd command for that folder.
Note that the
IF NOT EXIST statement checks if the folder contains any files or subfolders, so it will still execute the command if the folder itself is not empty but only contains empty subfolders.
Adding a Prefix or Suffix to Folder Names
Sometimes we may want to add a prefix or suffix to each folder name in the loop, for example, to differentiate between different runs of a process. We can achieve this by adding the prefix or suffix to the
%f variable using string manipulation.
Here’s an example syntax for adding a prefix to each folder name:
FOR /D %f IN (C:data*) DO processdata.cmd C:outputprefix_%~nxf
In the above command, we use the
%~nxf modifier to extract the folder name and extension from the
%f variable, and then add the prefix and output directory to the beginning of the folder name.
Similarly, we can add a suffix to each folder name by appending the suffix to the
FOR /D %f IN (C:data*) DO processdata.cmd C:output%~nxf_suffix
In the above command, we add the suffix to the end of the folder name using the
Using a Batch File for the Loop
While we can run the loop directly from the command prompt, it can be more convenient to create a batch file that we can run whenever we need to execute the command for each folder.
To create a batch file, we can open Notepad and type the loop command into a new file, then save the file with a
.bat extension. We can then double-click the batch file to run the loop.
Here’s an example batch file that loops over a directory’s folders and adds a prefix to each folder name:
FOR /D %%f IN (C:data*) DO (
ECHO Processing %%f
In the above batch file, we use the
@ECHO OFF statement to suppress the output of each command in the loop, and the
PAUSE command at the end to keep the command prompt window open after the loop has finished executing.
In this post, we have seen how to loop over a directory’s folders using Windows CMD and execute a command for each one. We have also seen how to exclude subfolders from the loop, add a prefix or suffix to folder names, and use a batch file for the loop.
By using these techniques, we can save time and automate repetitive tasks when working with large numbers of folders in Windows.
You can use the
for loop in the Windows command prompt to loop over the directories in a folder and perform a command on each one. Here’s an example of how you can do this:
for /D %i in (C:\data\*) do processdata.cmd %i
This will loop over all the directories in the
C:\data folder and run the
processdata.cmd command on each one, passing the directory name as an argument.
If you want to store the output of the
processdata.cmd command in a separate file for each directory, you can use the
>> operator to append the output to a file like this:
for /D %i in (C:\data\*) do processdata.cmd %i >> %i.txt
This will run the
processdata.cmd command on each directory and append the output to a file with the same name as the directory, located in the
Note that the
for loop syntax in the command prompt is different from that in other programming languages. In the command prompt, the
for loop variables are preceded by a percent sign (
%) and the
in keyword is used to specify the list of items to iterate over.
I hope this helps! Let me know if you have any questions.
for /D %G in ("c:\data\*") do processdata.cmd "%~fG"