Reputation: 1259
I'm currently facing the problem that I have to do an operation in every first dir "layer" of a directory.
I have a folder with thousands of sub dirs, I would just dor a for loop with /r but the problem is, those sub-dirs contain more sub-dirs an I don't want to go into those. For visualization:
Root Dir
----Sub-Dir 1
--------Sub-Dir 1 of Sub-Dir 1
--------Sub-Dir 2 of Sub-Dir 1
----Sub-Dir 2
--------Sub-Dir 1 of Sub-Dir 2
--------Sub-Dir 2 of Sub-Dir 2
----Sub-Dir 3
--------Sub-Dir 1 of Sub-Dir 3
--------Sub-Dir 2 of Sub-Dir 3
and I only want to go into the first "layer" Sub-Dir 1,2,3 etc. and don't touch the sub-sub-dirs of each.
Upvotes: 1
Views: 1148
Reputation: 130819
All you need are nested FOR /D statements (total of 2).
@echo off
pushd "rootDir"
call :doCommands
for /d %%F in (*) do (
pushd "%%F"
call :doCommands
for /d %%F in (*) do (
pushd "%%F"
call :doCommands
popd
)
popd
)
popd
exit /b
:doCommands
echo processing "%cd%"
exit /b
EDIT
Here is a generic solution that allows you to specify the root folder as arg1 (%1) and how many levels down to go as arg2 (%2).
@echo off
set currentLevel=0
set maxLevel=%2
if not defined maxLevel set maxLevel=0
:procFolder
pushd %1
echo processing "%cd%"
if %currentLevel% lss %maxLevel% (
for /d %%F in (*) do (
set /a currentLevel+=1
call :procFolder "%%F"
set /a currentLevel-=1
)
)
popd
Upvotes: 3