user2869231
user2869231

Reputation: 1541

Calling all Batch Files in a Folder

I'm trying to create a batch file that will execute all of the batch files located in a folder. So I have a folder that contains 6 (will increase) batch files, and I want to use a for loop to iterate through the list. An easier way of understanding it would be like so:

for batchFile in folder:
  CALL batchFile
  cd Desktop

Each batch file should start after the other finishes. Also, batch files will be the only type of files in the folder, so there shouldn't need to be any kind of separation. It may sound odd, but both the call and the directory change need to be inside of the loop (due to directory changes in each of the batch files). This is the closest thing I've found for getting a start on this:

 for /r %%i in "DIR" do CALL

Where "DIR" is the folder directory. However, it 1) doesn't work and 2) would only do a CALL and not also a directory change. Does anyone know how to do this?

Also, I'm using Windows 7.

Thanks.

EXTENSION:

Doing this in a bat file will work:

CALL "WelcomeScreenLists.bat"
cd directory
CALL "WelcomeScreenLinks.bat"
cd directory
CALL "RightClick.bat"
cd directory
CALL "RibbonButtons.bat"
cd directory
pause

"directory" is the full path of the folder containing the bat files. I would like to turn this into a for loop that loops through all files in the folder.

Upvotes: 2

Views: 1834

Answers (1)

pmbAustin
pmbAustin

Reputation: 3970

I would think you'd want something like this:

  for /f "delims=" %%i in ('dir /b ".\*.bat"') do (
    CALL %%i
  )

This works from the current directory. You can hard-code a directory by replacing the first "." in the dir statement with the full directory name, like so:

  for /f "delims=" %%i in ('dir /b "c:\folder\subfolder\*.bat"') do (
    CALL %%i
  )

The "for /f" syntax will iterate over file names, the "dir" command is any valid dir command... you can use any options to control sort order, for example.

Upvotes: 1

Related Questions