Batch script to execute some commands in each sub-folder

You can tell the batch to iterate directories:

for /d %i in (C:\temp\*) do ( cd "%i" &  *enter your command here* ) 

Use a percent sign when run directly on the command line, two when run from a batch

In a batch this would look something like this:

@echo off
set back=%cd%
for /d %%i in (C:\temp\*) do (
cd "%%i"
echo current directory:
cd
pause
)
cd %back%

Put the commands you need in the lines between ( and ).
If you replace C:\temp\ with %1 you can tell the batch to take the value of the directory from the first parameter when you call it.
Depending of the amount of directories you then either call the batch for each directory or read them from a list:

for /f %i in (paths.lst) do call yourbatch %i

The paths.lstwill look like this:

C:\
D:\
Y:\
C:\foo

All of this is written from memory, so you might need to add some quotations marks 😉
Please note that this will only process the first level of directories, that means no child folders of a selected child folder.

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)