Super User is a question and answer site for computer enthusiasts and power users. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I have two actions I'm trying to run using a simple .bat file. First time the file is run it proceeds to execute a command, second time it executes another... rinse and repeat.

Without knowing the inner workings I assume it will need to create a file to tell what state it is in. That's where my knowledge ends. What's a simple way to pull this off?

share|improve this question

There is. Using this post on stack overflow as an example I get:

(Create a file named first_run)

copy con first_run

(Create a batch file which check if first_run is present)

Copy con mycommand.bat
Welcome to my_command.bat

IF EXIST D:\first_run
start /wait first_command

echo Your first command has been executed. Terminating now.
goto :EOF

echo first_run marker not found. 
echo starting the second command.
start second_command

The goto end is ugly. I am sure there has to be a cleaner way. But it has been a long time since I used batch files.

Changed to goto :EOF. Thank you Karan, good tip.

share|improve this answer
If there are no subroutines (CALL), GOTO :EOF will work. – Karan Oct 28 '12 at 1:26

If I read the question correctly you want the batch file to run executableA.exe and then terminate. Next time it's run you want it to launch executableB.exe and terminate. Then the next time it runs you want it to run executableA.exe again.

A simple way would be to create a marker file like this:

@echo off
REM change the current working directory to where the batch script is located
cd /d %~dp0

if exist %~dp0Anext.txt (
  call :A
  goto :EOF
if exist %~dp0Bnext.txt (
  call :B
  goto :EOF
if not exist %~dp0*next.txt (
  call :firstrun
  goto :EOF
goto :EOF

REM subroutines only below this point:
REM you would launch executableA.exe here
echo AAA
echo B > Bnext.txt
del Anext.txt
goto :EOF

REM you would launch executableB.exe here
echo BBB
echo A > Anext.txt
del Bnext.txt
goto :EOF

REM you would launch executableA.exe here
echo AAA
echo B > Bnext.txt
goto :EOF

If you wanted to extend this you could make it rotate between three or more exectuables/scripts by following the same logic.

share|improve this answer

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .