Jump to content

I need a batch file to loop through .CMDs


fly

Recommended Posts

I know I've seen it here somewhere, but can't find it. Sadly, I suck at writing batch files, or I'd just do it myself.

I'm hoping to use the batch file to install all my apps...

Link to comment
Share on other sites


Is this what you mean?

for %%a in ("*.cmd") do %comspec% /c "%%~a"

<Edit>

You don't have to use '%comspec% /c', you may prefer to use 'call'

</Edit>

Edited by Yzöwl
Link to comment
Share on other sites

I will help answer that query.

Call is internal method to a execute a batch file. The batch file will pause, until the Call to the executed batch ends. The same instance of %ComSpec% does the operation for the entire loop.

%Comspec% is executing cmd.exe externally. This means that %ComSpec% is started For each loop. This is by far the slowest and inefficient method.

Link to comment
Share on other sites

Is this what you mean?
for %%a in ("*.cmd") do %comspec% /c "%%~a"

<Edit>

You don't have to use '%comspec% /c', you may prefer to use 'call'

</Edit>

Will that execute all .cmds in the directory, and only once?

EDIT: LOL, I guess I can't give this file name a .cmd, as it loops repeatedly then.

What does the /c do? As it just errored out for me.

Edited by fly
Link to comment
Share on other sites

An example:

Master.cmd

for %%a in ("Files\*.cmd") do Call "%%~a"

The above will execute the *.cmd files in the files directory, next to the script. Create a folder called files and create a cmd script with below in it.

Slave.cmd

@echo off
echo.
echo everything is working ok
echo.
pause

Now run the Master.cmd and you will see it execute the Slave.cmd. If you had other *.cmd scripts in files folder, then they would be executed also.

If you type cmd /? in the command console. It will tell you what /c does.

Carries out the command specified by string and then terminates

But as I have stated. Call is the best command for this purpose.

Link to comment
Share on other sites

Try this as a test, replace the pushd line with the location of your files

pushd C:\Documents and Settings\Administrator\Desktop\Batch Files
for %%a in ("*.cmd") do echo/call %%~na
pause&popd

When you've confirmed if and how the output works remove the echo/ and pause& and you're good to go.

Link to comment
Share on other sites

Yes, basically you use CALL MyBatch to call another batch file and CALL :MyLabel to do as you asked. The last command under your label should preferably say GOTO :EOF, to pass the command back to the next command after CALL :MyLabel.

  • Example

@ECHO OFF

ECHO. THIS IS MY FIRST ECHO COMMAND

CALL :SECOND

ECHO. THIS IS MY THIRD ECHO COMMAND

GOTO :ENDIT

:SECOND

ECHO. THIS IS MY SECOND ECHO COMMAND

GOTO :EOF

:ENDIT

ECHO. THIS IS THE END, GOODBYE

PAUSE

EXIT

Link to comment
Share on other sites

Yes, CALL accepts LABELs as a target. Type CALL /? at a command prompt for more information.

Yes, basically you use CALL MyBatch to call another batch file and CALL :MyLabel to do as you asked. The last command under your label should preferably say GOTO :EOF, to pass the command back to the next command after CALL :MyLabel.
  • Example

@ECHO OFF

ECHO. THIS IS MY FIRST ECHO COMMAND

CALL :SECOND

ECHO. THIS IS MY THIRD ECHO COMMAND

GOTO :ENDIT

:SECOND

ECHO. THIS IS MY SECOND ECHO COMMAND

GOTO :EOF

:ENDIT

ECHO. THIS IS THE END, GOODBYE

PAUSE

EXIT

Sorry, that wasn't in my mind. I asked the wrong question. The question should be put in this manner:

I have a batch file with multiple LABELs, how do I execute certain LABEL from outside of the batch file? eg. command prompt, registry...........

Link to comment
Share on other sites

I have a batch file with multiple LABELs, how do I execute certain LABEL from outside of the batch file? eg. command prompt, registry...........

You pass the label as a parameter:

BATCH1.CMD:

@ECHO OFF
CALL BATCH2.CMD 1stLABEL
pause
CALL BATCH2.CMD 2ndLABEL
:END

BATCH2.CMD

@ECHO OFF
::Following line checks for the parameter passed when calling the file, if it is not
::passed by the calling (batch or command) it ends programs
IF %1.==. GOTO :END

GOTO %1

:1stLABEL
ECHO This is 1stLABEL
PAUSE
GOTO :END

:2ndLABEL
ECHO This is 2ndLABEL
PAUSE
GOTO :END

:END
ECHO This is END of BATCH2.CMD
PAUSE

jaclaz

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...