Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

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

2 Answers 2

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

(Create a file named first_run)

copy con first_run
42
^z

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

Copy con mycommand.bat
Welcome to my_command.bat
pause  

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
^Z

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
1  
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:
:A
REM you would launch executableA.exe here
echo AAA
echo B > Bnext.txt
del Anext.txt
goto :EOF

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

:firstrun
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

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.