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?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

NVITEnd-user supportCommented:
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
NVITEnd-user supportCommented:
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
OWASP: Avoiding Hacker Tricks

Learn to build secure applications from the mindset of the hacker and avoid being exploited.

Ashley KirotAuthor Commented:
Thanks, it found the scripts in the current and sub directory's.

How do I run the found scripts?
0
Bill PrewIT / Software Engineering ConsultantCommented:
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 PrewIT / Software Engineering ConsultantCommented:
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 PrewIT / Software Engineering ConsultantCommented:
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
Bill PrewIT / Software Engineering ConsultantCommented:
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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Ashley KirotAuthor Commented:
Many thanks! That helped, it's not executing itself anymore, & It can recurse the sub directory and execute.
0
Bill PrewIT / Software Engineering ConsultantCommented:
Good, glad that helped.


»bp
0
Bill PrewIT / Software Engineering ConsultantCommented:
If you need help with the question close process take a look at:



»bp
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Windows Batch

From novice to tech pro — start learning today.