Windows Batch – Open Files with Same Extension in Sub-Folders

batchcmd.exewindows

I need to open a large number of files that have the same extension in one folder; these files are also in sub-folders inside this one folder.

How do I open all of them using CMD. The files are self-executable since they're .bat files and each executes specific commands when I double click them manually.

Best Answer

Give the below batch script a shot which uses a FOR /F loop and a CALL to execute each .bat file explicitly (with a CALL) starting from the RootDir location and traverse recursively from there to find and execute all .bat files in other subfolders beneath it.

Be sure to change the SET RootDir=C:\Folder variable value to the folder path you need to find the .bat files starting from it and looking through all subfolders within it which contain other .bat files you need to execute (i.e. SET RootDir=C:\OtherFolder).

Batch Script 1

@ECHO ON 
SET RootDir=C:\Folder

FOR /F "TOKENS=*" %%A IN ('DIR /S /B "%RootDir%\*.bat"') DO CALL "%%~A"
GOTO EOF

Batch Script 2

@ECHO ON 
SET RootDir=C:\Folder

CD /D "%RootDir%"
FOR /F "TOKENS=*" %%A IN ('DIR /S /B "*.bat"') DO CALL "%%~A"
GOTO EOF

Batch Script 3

@ECHO ON 
SET RootDir=C:\Folder

FOR /F "TOKENS=*" %%A IN ('DIR /S /B "%RootDir%\*.bat"') DO CMD /C "%%~A"
GOTO EOF

Batch Script 4

@ECHO ON 
SET RootDir=C:\Folder

FOR /F "TOKENS=*" %%A IN ('DIR /S /B "%RootDir%\*.bat"') DO START "" "%%~A"
GOTO EOF

Further Resources

Related Question