I'm working on writing a batch file to automate a long running process. At some points, the script may need to break out to allow for a human to look at and modify some data and address errors before picking back up where it left off. After a full day of troubleshooting, I was finally able to get this working (seems someone on stackoverflow had already had the same idea as me) but the working code is nearly identical to mine, and I don't understand why it works and mine doesn't. I'm wary of putting this batch file into production without fulling understanding what I've done. Any guidance would be appreciated.
I've tried many things, and have ultimately succeeded, as you'll see below. SETLOCAL EnableDelayedExpansion doesn't seem to change it. The thing that seems to be different is calling GOTO outside of the IF, but that doesn't seem as if it should be invalid. Eliminated things like file permissions, and used a explicit directory instead of the '.' in the variable. Called the directories explicitly instead of using variables... stuff like that.
This is working. When you break in the middle, the file is created as expected and the next run properly sets the variable and the GOTO sends us to the correct place:
@echo off
echo setting variables...
set BASEDIR=.
set STEPTRACKERFILE=%BASEDIR%\LASTSUCCESSFULSTEP.TXT
echo done.
if exist %STEPTRACKERFILE% (
echo step file found. resuming script...
set /p STEP=<%STEPTRACKERFILE%
) else (
set STEP=STEP1
)
GOTO %STEP%
:STEP1
echo step1
ECHO STEP2>%STEPTRACKERFILE%
pause
goto :eof
:STEP2
echo step2
ECHO STEP3>%STEPTRACKERFILE%
pause
goto :eof
:STEP3
echo step3
ECHO END>%STEPTRACKERFILE%
pause
goto :eof
:END
echo script end.
del /q %STEPTRACKERFILE%
pause
goto :eof
However, this nearly identical batch will work as long as the file doesn't exist, but if you break in the middle and run again, the variable is set to empty, and the goto fails because it's being passed a blank value:
@echo off
echo setting variables...
set BASEDIR=.
set STEPTRACKERFILE=%BASEDIR%\LASTSUCCESSFULSTEP.TXT
echo done.
if exist %STEPTRACKERFILE% (
echo step file found. resuming script...
set /p STEP=<%STEPTRACKERFILE%
echo step is %STEP%
GOTO %STEP%
)
:STEP1
echo step1
ECHO STEP2 > %STEPTRACKERFILE%
pause
:STEP2
echo step2
ECHO STEP3 > %STEPTRACKERFILE%
pause
:STEP3
echo step3
ECHO END > %STEPTRACKERFILE%
pause
:END
echo script end.
del /q %STEPTRACKERFILE%
pause
The error message just states the following:
step file found. resuming script...
step is
The system cannot find the batch label specified -
Any batch gurus left in the world that can tell me what's going on here?