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.
Ashley KirotAsked:
Who is Participating?
 
Bill PrewCommented:
Okay, I warned you about this potential problem, perhaps you didn't understand.

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.

And you posted:

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


Notice that the first script it found to execute is itself.  It will call itself and go into a loop doing that until it errors out.

Try this change to your script to make sure it doesn't execute itself:

@echo off
cd /d %~dp0

for /R %%s in (*.cmd) do (
    if /i "%%~s" NEQ "%~dpnx0" (
        call "%%~s"
    )
)

pause

Open in new window


»bp
0
 
NVITCommented:
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

0
 
Ashley KirotAuthor Commented:
Thanks for the response, i ran the code, again. Still received the same results. Any ideas what I'm doing wrong?
0
Introducing Cloud Class® training courses

Tech changes fast. You can learn faster. That’s why we’re bringing professional training courses to Experts Exchange. With a subscription, you can access all the Cloud Class® courses to expand your education, prep for certifications, and get top-notch instructions.

 
NVITCommented:
Try changing my code on line 43 from
call "%%~s"

Open in new window


to...
ECHO call "%%~s"

Open in new window


Then run the main batch. This will let you see what it's trying to CALL.
0
 
Ashley KirotAuthor Commented:
Thanks, it found the scripts in the current and sub directory's.

How do I run the found scripts?
0
 
Bill PrewCommented:
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
0
 
Ashley KirotAuthor Commented:
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

0
 
Bill PrewCommented:
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
0
 
Bill PrewCommented:
What is the "master script" file name?


»bp
0
 
Ashley KirotAuthor Commented:
"Run.cmd", is the master script.
I tried changing it back to, call "%%~s")
Same result.
0
 
Ashley KirotAuthor Commented:
Many thanks! That helped, it's not executing itself anymore, & It can recurse the sub directory and execute.
0
 
Bill PrewCommented:
Good, glad that helped.


»bp
0
 
Bill PrewCommented:
If you need help with the question close process take a look at:



»bp
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.