Link to home
Create AccountLog in
Avatar of Ashley Kirot
Ashley Kirot

asked on

Create A Batch Script To Run All Batch Scripts In Current And Sub Directory's

I've been trying to, create a batch script to run all batch scripts in the current and sub directory's. So far the current script only runs one batch in the current directory.

@echo off
cd /d %~dp0

for /R %%s in (*.cmd) do (
call "%%~s")
PAUSE

Open in new window


My apologies for any trouble, thanks in advance.
Avatar of NVIT
NVIT
Flag of United States of America image

Your code works fine here.

C:\local\test\EE\1>type 1.bat
@echo off
echo %0
pause

C:\local\test\EE\1>type 2.bat
@echo off
echo %0
pause

C:\local\test\EE\1>type 1\1.bat
@echo off
echo %0
pause

C:\local\test\EE\1>type 1\2.bat
@echo off
echo %0
pause

C:\local\test\EE\1>dir
 Volume in drive C is DCVXSBY1
 Volume Serial Number is 067D-CCDD

 Directory of C:\local\test\EE\1

04/05/2018  04:06 PM    <DIR>          .
04/05/2018  04:06 PM    <DIR>          ..
04/05/2018  04:03 PM    <DIR>          1
04/05/2018  04:01 PM                27 1.bat
04/05/2018  04:01 PM                27 2.bat
04/05/2018  04:08 PM               103 xx.bat
               3 File(s)            157 bytes
               3 Dir(s)  240,711,737,344 bytes free

C:\local\test\EE\1>type xx.bat
@echo off
setlocal enabledelayedexpansion

cd /d %~dp0

for /R %%s in (?.bat) do (
call "%%~s")

C:\local\test\EE\1>xx
"C:\local\test\EE\1\1.bat"
Press any key to continue . . .
"C:\local\test\EE\1\2.bat"
Press any key to continue . . .
"C:\local\test\EE\1\1\1.bat"
Press any key to continue . . .
"C:\local\test\EE\1\1\2.bat"
Press any key to continue . . .

Open in new window

Avatar of Ashley Kirot
Ashley Kirot

ASKER

Thanks for the response, i ran the code, again. Still received the same results. Any ideas what I'm doing wrong?
SOLUTION
Avatar of NVIT
NVIT
Flag of United States of America image

Link to home
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
See answer
Thanks, it found the scripts in the current and sub directory's.

How do I run the found scripts?
Please post one or more of the called BAT scripts, it's possible they are not returning back to the calling BAT script.

Also, be sure that the original script can not call itself otherwise that will lead to an infinite loop.  You may have to exclude that with an IF in the main script and make sure it doesn't try and execute itself.


»bp
There being echoed, just not executing.
call "C:\978978978\Run.cmd"
call "C:\978978978\Script_1.cmd"
call "C:\978978978\Execute\Script_2.cmd"
call "C:\978978978\Execute\Execute\Script_3.cmd"

Open in new window


The master batch script,
@echo off
cd /d %~dp0

for /R %%s in (*.cmd) do (
ECHO call "%%~s")
PAUSE

Open in new window

Correct, NVIT had you change it to just display the CALL statements, but not execute them.  If they look right then change:

ECHO call "%%~s")

to:

call "%%~s")


»bp
What is the "master script" file name?


»bp
"Run.cmd", is the master script.
I tried changing it back to, call "%%~s")
Same result.
ASKER CERTIFIED SOLUTION
Link to home
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
Many thanks! That helped, it's not executing itself anymore, & It can recurse the sub directory and execute.
SOLUTION
Link to home
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
If you need help with the question close process take a look at:



»bp