Skip to content
View in the app

A better way to browse. Learn more.

MSFN

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

I need a batch file to loop through .CMDs

Featured Replies

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

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

just quickly Yzöwl - whats the difference between %comspec% and call? i.e. pro's and con's

Just wondering :)

Cheers

N.

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.

  • Author
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

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.

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.

Can CALL execute a certain defined LABEL of a batch only instead of the whole batch file?

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

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

command a

command b command c

all above in one batch file..

and u wanna call a from outside?

u think too much...

remember u are under dos... and single task not multitask platform...

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

Create an account or sign in to comment

Recently Browsing 0

  • No registered users viewing this page.

Account

Navigation

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.