0

I would like to create a batch file (run.bat) to execute all files in a sub dir called execute.

I have seen a few examples on here but haven't succeeded in customising them for my requirements.

run.bat, and its sub folder might be anywhere. So a relative folder reference is needed.

e.g.

  • c:\somelocation\ Contains run.bat
  • c:\somelocation\execute\ Contains all the other batch files I want to run

It would be great too to be able to start them at 30 second intervals.

.bat programming is totally foreign to me so any help will be appreciated.

square_eyes
  • 1,269
  • 3
  • 22
  • 52

2 Answers2

1
@echo off
pushd c:\somelocation\execute
for /f "delims=" %%x in ('dir /b /a-d *.bat') do start "" "%%x"&timeout /t 30 >nul
popd

should get the job done. Switch to the location, execute each .bat file, wait 30 seconds after launching each one.


Edit : yes, should be timeout not choice. With choice, it needs parameters /t 30 /d y

Magoo
  • 77,302
  • 8
  • 62
  • 84
  • Great thanks. But this isn't relative to run.bat perhaps we need to use `%~dp0` as mentioned [here](http://lifehacker.com/5310255/use-relative-paths-in-your-batch-files). – square_eyes Jan 04 '14 at 17:19
  • I made that change (pushd %~dp0\execute) and it worked. But run.bat had an error `ERROR: Invalid syntax. /T can be specified only when /D is specified. Type "CHOICE /?" for usage.` – square_eyes Jan 04 '14 at 17:22
  • Also the 30 second pause was not adhered to. Both my test batch files ran at once. I assume that's related to the error. – square_eyes Jan 04 '14 at 17:27
  • Ah - your relative directory - use `.\execute`. `%~dp0` means 'the directory where the batch resides'. If you type `path` at the prompt, you'll be shown a list of directories separated by semicolons. This is the list of files used to locate an executable if it's not in the current directory. Choose any and then running `yourbatchname` will execute the batch regardless of your current directory - you don't need it everywhere. If you use batches a lot, you may consider adding a special directory (`batch` is popular, also `belfry`) and set your `path` to include that directory. – Magoo Jan 04 '14 at 17:36
  • I don't understand most of what you said. Nevertheless I tried `.\execute` which had the same results. It worked... but with the error above and no 30 second pause. – square_eyes Jan 04 '14 at 17:51
  • Have you implemented the change to the batch? `choice...` has become `timeout`. – Magoo Jan 04 '14 at 17:53
0

This uses the relative folder and adds a delay.

@echo off
for %%a in ("execute\*.bat") do (
   pushd "%%~dpa"
   call "%%~nxa"
   timeout /t 30 /nobreak >nul
   popd
)
pause
foxidrive
  • 40,353
  • 10
  • 53
  • 68
  • I can't get that to work. Running the bat just flashes a CMD window, and doesn't run my test bat in the sub-folder. – square_eyes Jan 06 '14 at 00:42
  • Place a `pause` command as the last line. If you are using XP or older then you will not have the `timeout` command and will have to use ping. – foxidrive Jan 06 '14 at 00:56
  • Pause keeps the first shell window up, but none of the .bat in the sub folder are run (they have pauses too). FYI I'm on Win7. – square_eyes Jan 06 '14 at 01:04
  • Thanks, but I get the same result – square_eyes Jan 06 '14 at 01:10
  • You made an error copying and pasting. It works fine. Or your folder is not called execute, or there are no bat files in the execute folder. If you see error messages on the screen then tell me what they are. – foxidrive Jan 06 '14 at 01:28