2

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.

6
  • Julia -- I added an answer for you so give it a shot when you get a chance and let me know if you find it of any usefulness for your need. You can use the below as a batch script or you can make a small change to the %%A and %%~A to make those use only one percent sign so.... %A and %~A and all should work from command line manually running that logic. Reply back when you get a chance and let me know. Aug 7, 2016 at 1:50
  • @PIMP_JUICE_IT I tried your script it didn't do anything. Actually, I didn't get an error message, I suppose something happened, does this script create a batch file somewhere on my computer that I can execute later? When I used single % instead of the double % only the first .bat file was executed. By the way, I need to run all of the batch files at once (parallel). Thank you so much for your help
    – Julia_arch
    Aug 7, 2016 at 2:38
  • Here is what I put C:\Users\Rania\AppData\Roaming\Ladybug my Ladybug folder has sub-folders that have the .bat files. and I ran the command from the Roaming folder.
    – Julia_arch
    Aug 7, 2016 at 2:41
  • I tried to run script 1,2, and 3 as an administrator. I got the same results as the initial results I got from running script 1 using both "%" and "%%". In fact, running script three I got this error "%%A was unexpected this time"
    – Julia_arch
    Aug 7, 2016 at 3:11

1 Answer 1

2

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

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .