1.

Solve : Launching all the .bat files in a folder one after the other?

Answer»

Hi,

I need to run a bunch of actions one after the other. All of them are in a different .bat file, all in the same folder. I need to create a .bat file that will take a look at the .bat files in the folder, launch the first one (alphabetically), WAIT for it to complete, then it would move that .bat file to an archive folder, then look again in the batch folder in case a new .bat file was added while the first one was working, and then launch the first batch file it finds, until the folder is empty from batch files.

I know how to launch a batch file, I know how to move a batch file from one folder to another, the part I need help with is to have a batch file look in the folder for .bat files, and launch the first one. I know how to look in the folder with the dir command, but how can I tell it to launch the first one in the list?

Also, how can I tell it that when there isn't any .bat file left in the folder it must stop?I found a way to get closer to the solution, but there is still a problem.

I should work with something like this:
Code: [Select]FOR /R Batch/ %%p IN (*.bat) DO START /B /WAIT %%p
But the problem is that since the Batch folder is located in folder that contain spaces, when it tries to launch the .bat file it's not able to because it stops looking at the first space it finds, so it tries to launch "E:Documents" instead of E:Documents and Settings/..."

So I tried replacing %%p with "%%p"

But now instead of giving me an error message it writes in the dos windows this:
Code: [Select]Microsoft Windows XP [Version 5.1.2600]
(C) Copyright 1985-2001 Microsoft Corp.
Anyone would have a solution for that?I found the solution!!

It's because when you put your variable between "" it things that this is the title, so you just need to put whatever title you want in front of your variable.

Like this:
Code: [Select]FOR /R Batch/ %%p IN (*.bat) DO START /B /WAIT "TITLE" "%%p" Quote

put whatever title you want in front of your variable.

Such as an empty string (2 quote marks)



Code: [Select]FOR /R Batch/ %%p IN (*.bat) DO START /B /WAIT "" "%%p" Quote from: FausseFugue on July 02, 2008, 02:24:52 PM
I found the solution!!

Maybe I'm just tired but I fail to SEE how this single line of code is the solution to the original request.

Quote
FOR /R Batch/ %%p IN (*.bat) DO START /B /WAIT "" "%%p"

Where exactly is the logic that would:
Quote
then look again in the batch folder in case a new .bat file was added while the first one was working, and then launch the first batch file it finds, until the folder is empty from batch files.

Where is the logic that would:
Quote
then it would move that .bat file to an archive folder

Why is recursion used when all the batch files are in the same folder?

Why is start used instead of call?

Just curious.

Hi Sidewinder,

Actually I said that I found the solution because I found the solution to the part I mentionned was problematic in the first message, the rest I already knew how to do it.

About the part where it looks again in the folder, I need to figure it out, I don't know how to ask with bat code "is the folder empty from .bat files?".

Here is the full code I'm using, with the part where it moves the file:

Code: [Select]ECHO OFF

CD /d "%~dp0"


FOR /R Batch/ %%p IN (*.bat) DO (
START /B /WAIT "TITLE" "%%p"
MOVE /Y "%%p" "Batch Archive/")
About the call instead of start, I didn't know about the call command, I'm gonna look into that.

And about the recursion used... what is recursion exactly?

Thanks,This bit  of doggeral  may help you out. It rechecks the directory contents and uses redirection to INDICATE the directory is empty.

Code: [Select]echo off
cd /d c:\batch

:loop
for /f "tokens=* delims=" %%v in ('dir *.bat /b 2^>^&1') do (
if "%%p"=="File Not Found" goto :eof
call "%%p"
move /y "%%p" c:\batch\archive
)
goto loop

Feel free to change the directory names.

Recursion is a function that calls itself. Used in your posted code, it refers to walking down the directory tree, including all files and all FOLDERS within all folders. Example executing dir c:\ /a /s at a command prompt will list every file and folder on the c: drive. Some Windows commands have a switch for recursion where in most script languages the user must write the logic themselves.

 Thanks Sidewinder,

I'll check you code when I have a minute.

About the recursion, did you mean this line of code?:
Code: [Select]CD /d "%~dp0"
If that's what you meant, I'm using this because I'm launch the .bat file from a javascript. And it's actually the javascript that is creating the .bat file. And the problem with launching a .bat file from a javascript is that for some REASON the .bat file doesn't know where to place the current directory when it starts, so I need to tell it to use it's own directory.

Or maybe you meant the fact that my code didn't only look inside the Batch folder but also in subfolder, if that's what you meant, then, I did it that way because I didn't know how to do it another way. But I checked you code fast and it seems to solve that!

Thanks!Actually the recursion is in this statement (the R switch specifically):

Quote
FOR /R Batch/ %%p IN

The for command has more than a few variations. Check out for /? for details.

 Thanks for the help Sidewinder,

I checked the help on this site for the for command. It really help me understand what was going on. I had took the line of code I was using someplace else but I didn't know exactly what all those letters were for, but now it's much clearer. All my code seems good now. My little software is working perfectly.

Thank you!


Discussion

No Comment Found