Titanium
Titanium

Reputation: 81

Batch file to run all .bat files in folders and sub-folders with different names

I need a simple script to run all .bat files in folders and sub-folders with their different names. here is an example :

Main Folder> Bat Folder 1> Bat File 1.bat

Main Folder> Bat Folder 2> Bat File 2.bat

. . .

Main Folder> Bat Folder N> Bat File N.bat

There are many topics out there asking the same question but the one that really worked for me was the following :

@echo off
pushd C:\Users\%USERNAME%\Desktop\Bat Folder 1\
for /f "delims=" %%x in ('dir /b /a-d *.bat') do start "" "%%x"&timeout /t 2 >nul
popd

However the problem is the direct folder address. I can't manually enter the folder names and run them one by one. it would take a long time. I want the script to go through all folders and sub-folders ignoring their names and run them all. it would be better to have the script run inside the main folder instead of the folder address !

Upvotes: 0

Views: 4664

Answers (3)

avery_larry
avery_larry

Reputation: 2135

Not a full answer, but I think this might help give you a start:

for /r "C:\Users\%USERNAME%\Desktop" %%a in (*.bat) do start "" "%%~a"

You might need call "something.bat" or start "" cmd /c "something.bat"

I suppose, if your batch files need a working directory, you could try this:

:: Make sure you're in the correct drive
c:
for /r "C:\Users\%USERNAME%\Desktop" %%a in (*.bat) do (
   cd "%%~pa"
   call "%%~a"
)

Upvotes: 3

Titanium
Titanium

Reputation: 81

I found an alternative. I used the code dir /s /b *.bat > runall.bat to get the full directory list and then replaced them all with pushd C:\Users\%USERNAME%\Desktop\Bat Folder 1\Bat File 1.bat

It's definitely not a good way to go but at least it fixed my issue.

Edit: It simply doesn't work. just reads the first line and ignores the rest.

Upvotes: 0

AlexP
AlexP

Reputation: 4430

pushd "\Path\To\Main Folder"
for /r %%F in ("*.bat") do start /wait "" "%%~F"

for /r will iterate recursively over all *.bat files in all subdirectories to any depth.

Upvotes: 0

Related Questions