Q. How can I run commands for every specifically named sub-folder of a directory tree?

I have scripted RunFolder.bat to run a batch file on all the sub-folders of a parent folder that match a specified mask.

The syntax for using RunFolder.bat is:

RunFolder StartPath SubFolder Batch

Where:

StartPath is the fully qualified path to the parent folder.

SubFolder is the mask used to find sub-folders.

Batch     is the fully qualified batch file name to run, 
          passing it the fully qualified name of the sub-folder.
RunFolder.bat contains:
@echo off
setlocal
if \{%3\}==\{\} goto syntax
set folder=%1#
set folder=%folder:"=%
set folder=%folder:\#=%
set folder=%folder:#=%
if not exist "%folder%\*.*" goto syntax
set sub=%2
set sub=%sub:"=%
if not exist %3 goto syntax
set batch=%3
for /f "Tokens=*" %%d in ('dir "%folder%" /b /s /ad ^|Findstr /I /c:"%sub%"') do call :isSub "%%d"
endlocal
exit /b 0
:syntax
@echo Syntax RunFolder StartPath SubFolder Batch
endlocal
exit /b 1
:isSub
set dir=%1
set parent=%~DP1
call set work=%%dir:%parent%=%%
if /i %work% NEQ "%sub%" goto :EOF
call %batch% %dir%


Hide comments

Comments

  • Allowed HTML tags: <em> <strong> <blockquote> <br> <p>

Plain text

  • No HTML tags allowed.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Lines and paragraphs break automatically.
Publish