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.
Windows Batch

Avatar of undefined
Last Comment
Bill Prew
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

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
Avatar of Ashley Kirot
Ashley Kirot

ASKER

Thanks, it found the scripts in the current and sub directory's.

How do I run the found scripts?
Avatar of Bill Prew
Bill Prew

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
Avatar of Ashley Kirot
Ashley Kirot

ASKER

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

Avatar of Bill Prew
Bill Prew

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
Avatar of Bill Prew
Bill Prew

What is the "master script" file name?


»bp
Avatar of Ashley Kirot
Ashley Kirot

ASKER

"Run.cmd", is the master script.
I tried changing it back to, call "%%~s")
Same result.
ASKER CERTIFIED SOLUTION
Avatar of Bill Prew
Bill Prew

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
Avatar of Ashley Kirot
Ashley Kirot

ASKER

Many thanks! That helped, it's not executing itself anymore, & It can recurse the sub directory and execute.
SOLUTION
Avatar of Bill Prew
Bill Prew

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
Avatar of Bill Prew
Bill Prew

If you need help with the question close process take a look at:



»bp
Windows Batch
Windows Batch

Batch files are text files containing a script of commands that are executed by the command interpreter on DOS, OS/2 and Windows systems. Most commonly, they are used to perform a series of functions that are repeated -- copying a set of files created daily with one step, for example.

13K
Questions
--
Followers
--
Top Experts
Get a personalized solution from industry experts
Ask the experts
Read over 600 more reviews

TRUSTED BY

IBM logoIntel logoMicrosoft logoUbisoft logoSAP logo
Qualcomm logoCitrix Systems logoWorkday logoErnst & Young logo
High performer badgeUsers love us badge
LinkedIn logoFacebook logoX logoInstagram logoTikTok logoYouTube logo