Batch File to Loop a VBScript During Certain Times of the Day - windows

I am very new to batch, so please be easy. That being said, I am attempting to write a batch file that every few minutes will execute a VBScript that updates information on our network. Currently I have:
#ECHO OFF
REM Run VBScript
GOTO SKIP01
:LOOP
wscript "C:\Users\Desktop\RenameMove.vbs"
GOTO SKIP01
:
:
:SKIP01
REM 3 Min Delay
PING 1.1.1.1 -n 10 -w 18000 >NUL
IF HOUR:%time:~0,5% <09:44
GOTO SKIP01
IF HOUR:%time:~0,5% >16:00
GOTO SKIP01
IF HOUR:%time:~0,5% >=09:45
GOTO LOOP
Currently the window will open for a minute or two, then it just closes. If I run the above after removing the first GOTO SKIP01, the VBScript executes perfectly. Then there is a delay and the window closes. I imagine that it is everything under :SKIP01 that is causing the problem. The function I am trying to achieve is for the .BAT file to continuously loop a delay between the hours of 16:01 - 09:44. Then run the VBScript every 3 minutes from 09:45 - 16:00. Searching the forums I have yet to find anything to help. As I said, I am very new to Batch.
RE: My last comment below:
#ECHO OFF
setlocal enableextensions disabledelayedexpansion
set delay=180
set "startTime=09:45"
set "endTime=16:00"
:LOOP
set "now=%time: =0%"
:: ECHO It's %now%, waiting %delay% seconds
echo %date% %time% >> log.txt
if "%now%" geq "%startTime%:00,00" (
GOTO LOOP2
)
:LOOP2
set "now=%time: =0%"
:: ECHO It's %now%, waiting %delay% seconds
echo %date% %time% >> log.txt
if "%now%" lss "%endTime%:00,00" (
:: ECHO Start MSAccess
GOTO CALLVBS
)
:WAIT
REM 10 second delay
PING 127.0.0.1 -n %delay% >REM
GOTO LOOP
:CALLVBS
echo Time to Move!
wscript "C:\Users\ljs\Desktop\Stock_Automation_DO_NOT_EDIT\RenameMove.vbs"
GOTO WAIT

I renamed some things so it made more sense to me, but here is a working version. there are variables at the top for the delay (in seconds) and the start and end time for the window. I also modified your ping delay to something more simple, in my opinion.
Remove the echos for production and add the vbscript file to the CALLVBS function.
#ECHO OFF
setlocal enableextensions disabledelayedexpansion
set delay=180
set "startTime=09:45"
set "endTime=16:00"
:LOOP
set "now=%time: =0%"
ECHO It's %now%, waiting %delay% seconds
if "%now%" geq "%startTime%:00,00" (
ECHO It's after %startTime%
if "%now%" lss "%endTime%:00,00" (
ECHO And it's before %endTime%
GOTO CALLVBS
)
)
:WAIT
REM 10 second delay
PING 127.0.0.1 -n %delay% >REM
GOTO LOOP
:CALLVBS
echo It's time!
REM Call VBS here
GOTO WAIT
Here's what's happening, as explained by someone who barely knows what he's talking about:
ABOUT GOTO
First you need to know that batch files process line-by-line from top to bottom, unless it encounters certain statements like if, for or goto, the last of which being the one we are concerned with here. If the interpreter encounters a GOTO command, it will go to the corresponding label and resume processing code line by line until it finds another GOTO or gets to the end of the file.
SIMPLE EXAMPLE
#echo off
GOTO :FRUITS
:COLORS
echo Red
echo Green
GOTO :END
:FRUITS
echo Apple
echo Banana
GOTO :COLORS
:END
echo Done!
This outputs the following:
Apple
Banana
Red
Green
Done!
BREAKDOWN
Set up variables
#ECHO OFF
setlocal enableextensions disabledelayedexpansion
set delay=180
set "startTime=09:45"
set "endTime=16:00"
This sets some settings and creates some variables for use later. The variables should be self explanatory but
I can elaborate if you want.
check the time
01. :LOOP
02. set "now=%time: =0%"
03. ECHO It's %now%, waiting %delay% seconds
04. if "%now%" geq "%startTime%:00,00" (
05. ECHO It's after %startTime%
06. if "%now%" lss "%endTime%:00,00" (
07. ECHO And it's before %endTime%
08. GOTO CALLVBS
09. )
10. )
This is our "loop". :LOOP denotes what is basically a labeled
section of code that we can go back to any time we want. I called it LOOP
because it is the section we are doing over and over. It may have been more
accurate to call it :CHECKTIME or something similar, as that's what it does.
The label means absolutely nothing to the interpreter so calling it "LOOP"
doesn't mean it's going to repeat. This may be the biggest source of confusion.
Here is a
step-by-step of what each line on this block does (not processing
conditions, just line by line):
Get the current time.
Output the current time
Compare the current time to the endTime variable.
Output the result.
Compare the current time to the startTimevariable.
Output the result.
Goto the CALLVBS section of code
Note that I could have put GOTO WAIT at the end of this block and that might make more sense, but since :WAIT is the next block of
code that's what will process next anyway, so the GOTO would be
superfluous! This may be a second point of confusion.
Wait a bit
:WAIT
REM 10 second delay
PING 127.0.0.1 -n %delay% >REM
GOTO LOOP
This is the section of code that simply waits the specified number of
seconds. It does this using the ping command which is common for
batch programming since there is no built-in delay or sleep command
like other languages have. The first line is simply a comment, REM
means "Remove" (I think) and is how you comment out lines of code in
batch. As a matter of fact, I should have removed this since it's
not 10 seconds anyway :). The second line pings the localhost 180
times (or whatever the delay variable is set to). The >REM part
means it outputs the results of the ping to, well, I've never seen
"REM" here. Usually you would output it to nul but either way, it's
making sure you don't see the 180 ping results. Now, the 3rd line
tells the processor to go back to the :LOOP label. No matter what.
After the ping, it does the :LOOP section code again.
Execute your vbscript
:CALLVBS
echo It's time!
REM Call VBS here
GOTO WAIT
This is the :CALLVBS section of code. First it outputs "It's
time!". The second line is again, a comment. As you know, you
replace this with your vbscript. After this, the interpreter is told
to go to the :WAIT section of code. Again, it will always do this,
no matter what, after executing the line above it.

Related

Second Timeout function didn`t work in Batch File

Working on a startup File (like a shortcut with special functions) for a game. (Counter-Strike Global Offensive)
The intention is to replace the menu music - some updates ago this was easier but right now I need to forging on a Batch file.
This is how it looks so far
#Echo off
"C:\Users\Cedo\Desktop\csgo\csgo\Counter-Strike Global Offensive.url"
Timeout 9
"C:\Users\Cedo\Desktop\csgo\csgo\csgomusic.m3u8"
[Timeout 240]!
[Taskkill /IM winamp.exe]! ----> Dont work!
Exit
and the way to here is more complicated than I thought. I solved already a lot of problems because a lot of the stuff on the internet seems outdated I guess.
The actual problem is a second timeout and taskkill for the winamp music file.
Strangely when inserting a second timeout (like the first) nothing happens.
Just after closing winamp manually the Timer starts!
But something needs to be done so that the second timeout with 240 secs and the taskkill to close winamp automatically works.
Another cool thing would be to start the winamp music file a bit later without minimizing the started game window.
(Already tried start /min commands - it doesn't matter actually - wont work if minimized or maximized)
Does someone know more? Right now I'm good with 9 Seconds(winamp opens before the game maximizes because this takes some time and so i didn`t get tabbed out).
And is there maybe a better way to build this up or improve things?
Just after closing winamp manually the Timer starts!
Given this statement I infer that "C:\Users\Cedo\Desktop\csgo\csgo\csgomusic.m3u8" is starting Winamp.
Note place the following script into the "C:\Users\Cedo\Desktop\csgo\csgo\" folder, or update the "_RootFolder" to have the correct path.
So essentially what you need to do is this:
Start a separate command window to run the task to kill winamp, because otherwise you will only move on tot the next command when Winamp is killed manually.
#(SETLOCAL
REM SET "KillCmd="%~f0" :Kill_Winamp"
REM SET "_StartKillCMD=start "Kill Winamp!" cmd /c %KillCmd%"
SET "_StartKillCMD=start "Kill Winamp!" cmd /c "%~f0" :Kill_Winamp"
SETLOCAL EnableDelayedExpansion
ECHO OFF
SET "eLvl=0"
REM SET "_RootFolder=%USERPROFILE%\Desktop\csgo\csgo\"
SET "_RootFolder=%~dp0"
SET "CSGo_URL=!_RootFolder!Counter-Strike Global Offensive.url"
SET "Music_File=!_RootFolder!csgomusic.m3u8"
SET /A "_Timer_WinAmp_Start=9", "_Timer_WinAmp_Kill= _Timer_WinAmp_Start + 240"
)
ECHO=%* | FIND /I ":Kill_Winamp" && (
CALL :Kill_Winamp
) || (
CALL :Main
)
( ENDLOCAL
Exit /b %_eLvl%
}
:Main
COLOR 2F
ECHO=======================================================================
ECHO= Launching CSGO, Waiting %_Timer_WinAmp_Start% seconds before starting Winamp.
ECHO=======================================================================
ECHO=
"%CSGo_URL%"
ECHO=
SETLOCAL DisableDelayedExpansion
%_StartKillCMD%
ECHO=
ECHO=Started Kill Timer..
ECHO=
Timeout %_Timer_WinAmp_Start%
"%Music_File%"
ECHO=Exiting, Remove the pause on the next line if not needed.
Pause
GOTO :EOF
:Kill_Winamp
COLOR 4F
ECHO=======================================================================
ECHO= Waiting %_Timer_WinAmp_Kill% Seconds before Killing Winamp.
ECHO=======================================================================
ECHO=
ECHO=
Timeout %_Timer_WinAmp_Kill%
Taskkill /IM winamp.exe
ECHO=Exiting, Remove the pause on the next line if not needed.
Pause
GOTO :EOF

Batch File: Getting user input during execution of other commands, then checking it later

What I want is a command (or series of commands) that works with Windows 7, 8.1, and 10. It needs to collect user input at any time during the execution of the batch file it's in, only to checked and interpreted later. I do not mind using an input text file.
I've already searched for an answer, but the closest I've come to it is <nul set /p "input=", but it requires the user to press a key and hit enter at the exact moment the command is run. Any help would be greatly appreciated.
This method utilizes GOTO to create a loop which checks the first line of input.txt every 6 seconds or so. You could replace the content of :DOSTUFF with anything you want. Let me know if you have questions.
#echo off
GOTO DOSTUFF
:CHECKINPUT
for /f %%a in (input.txt) do (
if %%a NEQ "" (
set "input=%%a"
GOTO GOTINPUT
)
exit /b
)
GOTO DOSTUFF
:GOTINPUT
echo Thanks for the input!
echo Here is what you entered:
echo %input%
GOTO ENDER
:DOSTUFF
echo I could be doing other things here, but instead I'm just waiting for input...
PING 127.0.0.1 -n 6 >nul
GOTO CHECKINPUT
:ENDER
pause
While this was running in one window, I just ran echo test>input.txt in another command prompt.
To make this more robust you might want to overwrite the file after you check it. This can easily be done with echo.>input.txt

Variable not getting set correctly in a dos batch file on XP

I am trying to write a batch file to be run in a command prompt on XP. I am trying to get a listing of files in a specific path that follow a certain naming convention. I need to copy and rename each file instance to a static name and drop it to a transmission folder.
Since it may take a little while for the file to go in the transmission folder, I need to check before I copy the next file over so that I don't overlay the previous file. I am not able to use SLEEP or TIMEOUT since I don't have the extra toolkit installed. I try to just continually loop back to a START section until the file is sent.
I noticed that if I passed the %%x value set in the for loop that if I loop back to the START section a couple of times, it seems to lose its value and it is set to nothing. So I tried to set a variable to hold the value.
I seem to be having issues with the variable not being set correctly or not cleared. Originally it kept on referencing the first file but now it doesn't seem to be set at all. The ECHO displays the correct the value but the filename variable is empty still.
Does anyone know a better way of doing this? Thanks in advance for your help as I have already wasted a whole day on this!
This is the batch file:
#ECHO "At the start of the loop"
#for %%x in (C:\OUTBOUND\customer_file*) do (
#ECHO "In the loop"
#ECHO "loop value ="
#ECHO %%x
SET filename=%%x
#ECHO "filename ="
#ECHO %filename%
#ECHO ...ARCHIVE OUTBOUND CUSTOMER FILE
archivedatafile --sourcefile="%filename%" --archivefolder="..\archivedata\customer" --retentiondays=0
IF NOT %ERRORLEVEL%==0 GOTO ERROR
PAUSE
:START
IF EXIST l:\OutputFile (
#ping 1.1.1.1 -n 1 -w 30000
GOTO START
) ELSE (
COPY %filename% l:\OutputFile /Y
IF NOT %ERRORLEVEL%==0 GOTO ERROR
PAUSE
)
)
GOTO END
:ERROR
#echo off
#ECHO *************************************************************
#ECHO * !!ERROR!! *
#ECHO *************************************************************
:END
SET filename=
foxidrive has provided a script that should work, but did not provide an explanation as to why your code fails and how he fixed the problems.
You have 2 problems:
1) Your FOR loop is aborted immediately whenever GOTO is executed within you loop. It does not matter where the GOTO target label is placed - GOTO always terminates a loop. Foxidrive's use of CALL works perfectly - the loop will continue once the CALLed routine returns.
2) You attempt to set a variable within a block of code and then reference the new value within the same block. %VAR% is expanded when the statement is parsed, and complicated commands like IF and FOR are parsed once in their entirety in one pass. Actually, any block of code within parentheses is parsed in one pass. So the values of %ERRORLEVEL% and %FILENAME% will be constant - the values that existed before the block was entered.
As Endoro has indicated, one way to solve that problem is to use delayed expansion. Delayed expansion must be enabled by using setlocal enableDelayedExpansion, and then expand the variable using !VAR!. The value is expanded at execution time instead of parse time. Type HELP SET from the command prompt for more information about delayed expansion.
But beware that delayed expansion can cause its own problems when used with a FOR loop because the delayed expansion occurs after the FOR variable expansion: %%x will be corrupted if the value contains a !. This problem can be solved by carefully toggling delayed expansion ON and OFF as needed via SETLOCAL and ENDLOCAL.
Foxidrive's code avoids the entire delayed expansion issue by using CALL. His :NEXT routine is not inside a FOR loop, so all the commands are reparsed each time it is called, so delayed expansion is not required.
This may work - it is untested:
#echo off
ECHO Starting...
for %%x in (C:\OUTBOUND\customer_file*) do call :next "%%x"
echo done
pause
goto :eof
:next
ECHO ...ARCHIVING OUTBOUND CUSTOMER FILE "%~1"
archivedatafile --sourcefile="%~1" --archivefolder="..\archivedata\customer" --retentiondays=0
IF ERRORLEVEL 1 GOTO :ERROR
:loop
echo waiting for file...
ping -n 6 localhost >nul
IF EXIST l:\OutputFile GOTO :loop
COPY "%~1" l:\OutputFile /Y
IF ERRORLEVEL 1 GOTO :ERROR
GOTO :EOF
:ERROR
ECHO *************************************************************
ECHO * !!ERROR!! in "%%x"
ECHO *************************************************************
pause
goto :EOF
try this:
#echo off&setlocal
for %%x in (C:\OUTBOUND\customer_file*) do SET "filename=%%x"
ECHO %filename%
ECHO ...ARCHIVE OUTBOUND CUSTOMER FILE
archivedatafile --sourcefile="%filename%" --archivefolder="..\archivedata\customer" --retentiondays=0
IF NOT %ERRORLEVEL%==0 GOTO:ERROR
PAUSE
:START
IF EXIST l:\OutputFile ping 1.1.1.1 -n 1 -w 30000&GOTO:START
COPY "%filename%" l:\OutputFile /Y
IF NOT %ERRORLEVEL%==0 GOTO:ERROR
PAUSE
GOTO:END
:ERROR
echo off
ECHO *************************************************************
ECHO * !!ERROR!! *
ECHO *************************************************************
:END
SET "filename="
If you use codeblocks (if and for with ( )) and variables with changing values you have to enable delayed expansion. You don't need code blocks in this code, as you can see.

Windows batch: sleep [duplicate]

This question already has answers here:
Sleeping in a batch file
(34 answers)
Closed 9 years ago.
How do I get a Windows batch script to wait a few seconds?
sleep and wait don't seem to work (unrecognized command).
You can try
ping -n XXX 127.0.0.1 >nul
where XXX is the number of seconds to wait, plus one.
I don't know why those commands are not working for you, but you can also try timeout
timeout <delay in seconds>
timeout /t 10 /nobreak > NUL
/t specifies the time to wait in seconds
/nobreak won't interrupt the timeout if you press a key (except CTRL-C)
> NUL will suppress the output of the command
To wait 10 seconds:
choice /T 10 /C X /D X /N
Microsoft has a sleep function you can call directly.
Usage: sleep time-to-sleep-in-seconds
sleep [-m] time-to-sleep-in-milliseconds
sleep [-c] commited-memory ratio (1%-100%)
You can just say sleep 1 for example to sleep for 1 second in your batch script.
IMO Ping is a bit of a hack for this use case.
For a pure cmd.exe script, you can use this piece of code that returns the current time in hundreths of seconds.
:gettime
set hh=%time:~0,2%
set mm=%time:~3,2%
set ss=%time:~6,2%
set cc=%time:~-2%
set /A %1=hh*360000+mm*6000+ss*100+cc
goto :eof
You may then use it in a wait loop like this.
:wait
call :gettime wait0
:w2
call :gettime wait1
set /A waitt = wait1-wait0
if !waitt! lss %1 goto :w2
goto :eof
And putting all pieces together:
#echo off
setlocal enableextensions enabledelayedexpansion
call :gettime t1
echo %t1%
call :wait %1
call :gettime t2
echo %t2%
set /A tt = (t2-t1)/100
echo %tt%
goto :eof
:wait
call :gettime wait0
:w2
call :gettime wait1
set /A waitt = wait1-wait0
if !waitt! lss %1 goto :w2
goto :eof
:gettime
set hh=%time:~0,2%
set mm=%time:~3,2%
set ss=%time:~6,2%
set cc=%time:~-2%
set /A %1=hh*360000+mm*6000+ss*100+cc
goto :eof
For a more detailed description of the commands used here, check HELP SET and HELP CALL information.
Heh, Windows is uhm... interesting. This works:
choice /T 1 /d y > NUL
choice presents a prompt asking you yes or no. /d y makes it choose yes. /t 1 makes it wait a second before typing it. > NUL squashes output.
The Windows 2003 Resource Kit has a sleep batch file. If you ever move up to PowerShell, you can use:
Start-Sleep -s <time to sleep>
Or something like that.
I rely on JScript. I have a JScript file like this:
// This is sleep.js
WScript.Sleep( WScript.Arguments( 0 ) );
And inside a batch file I run it with CScript (usually it is %SystemRoot%\system32\cscript.exe)
rem This is the calling inside a BAT file to wait for 5 seconds
cscript /nologo sleep.js 5000
I just wrote my own sleep which called the Win32 Sleep API function.
RJLsoftware has a small utility called DelayExec.exe. With this you can execute a delayed start of any program in batches and Windows registry (most useful in ...Windows/.../Run registry).
Usage example:
delayexec "C:\WINDOWS\system32\notepad.exe" 10
or as a sleep command:
delayexec "nothing" 10
Personally I use a Perl one-liner:
perl -e "sleep 10;"
for a 10-second wait. Chances are you'll already have Perl installed on a development machine as part of your git installation; if not you will have to install it, for example, from ActiveState or Strawberry, but it's one of those things I install anyway.
Alternatively, you can install a sleep command from GnuWin32.

Changing a batch file when its running

I am running a long running batch file. I now realize that I have to add some more commands at the end of the batch file (no changes to exisiting content, just some extra commands). Is it possible to do this, given that most batch files are read incrementally and executed one by one? Or does the system read the entire contents of the file and then runs the job?
I just tried it, and against my intuition, it picked up the new commands at the end (on Windows XP)
I created a batch file containing
echo Hello
pause
echo world
I ran the file, and while it was paused, added
echo Salute
Saved it and pressed enter to contine the pause, all three prompts were echoed to the console.
So, go for it!
The command interpreter remembers the line position byte offset it's at in the batch file. You will be fine as long as you modify the batch file after the current executing line position byte offset at the end of the most recently parsed line of code.
If you modify it before then it will start doing strange things (repeating commands etc..).
jeb's example is a lot of fun, but it is very dependent on the length of the text that is added or deleted. I think the counter-intuitive results are what rein meant when he said "If you modify it before then it will start doing strange things (repeating commands etc..)".
I've modified jeb's code to show how dynamic code of varying length can be freely modified at the beginning of an executing batch file as long as appropriate padding is in place. The entire dynamic section is completely replaced with each iteration. Each dynamic line is prefixed with a non interfering ;. This conveniently allows FOR /F to strip the dynamic code because of the implicit EOL=; option.
Instead of looking for a particular line number, I look for a specific comment to locate where the dynamic code begins. This is easier to maintain.
I use lines of equal signs to harmlessly pad the code to allow for expansion and contraction. Any combination of the following characters could be used: comma, semicolon, equal, space, tab and/or newline. (Of course the padding cannot begin with a semicolon.) The equal signs within the parentheses allow for code expansion. The equal signs after the parentheses allow for code contraction.
Note that FOR /F strips empty lines. This limitation could be overcome by using FINDSTR to prefix each line with the line number and then strip out the prefix within the loop. But the extra code slows things down, so it's not worth doing unless the code is dependent on blank lines.
#echo off
setlocal DisableDelayedExpansion
echo The starting filesize is %~z0
:loop
echo ----------------------
::*** Start of dynamic code ***
;set value=1
::*** End of dynamic code ***
echo The current value=%value%
::
::The 2 lines of equal signs amount to 164 bytes, including end of line chars.
::Putting the lines both within and after the parentheses allows for expansion
::or contraction by up to 164 bytes within the dynamic section of code.
(
call :changeBatch
==============================================================================
==============================================================================
)
================================================================================
================================================================================
set /p "quit=Enter Q to quit, anything else to continue: "
if /i "%quit%"=="Q" exit /b
goto :loop
:changeBatch
(
for /f "usebackq delims=" %%a in ("%~f0") do (
echo %%a
if "%%a"=="::*** Start of dynamic code ***" (
setlocal enableDelayedExpansion
set /a newValue=value+1, extra=!random!%%9
echo ;set value=!newValue!
for /l %%n in (1 1 !extra!) do echo ;echo extra line %%n
endlocal
)
)
) >"%~f0.tmp"
::
::The 2 lines of equal signs amount to 164 bytes, including end of line chars.
::Putting the lines both within and after the parentheses allows for expansion
::or contraction by up to 164 bytes within the dynamic section of code.
(
move /y "%~f0.tmp" "%~f0" > nul
==============================================================================
==============================================================================
)
================================================================================
================================================================================
echo The new filesize is %~z0
exit /b
The above works, but things are much easier if the dynamic code is moved to a subroutine at the end of the file. The code can expand and contract without limitation, and without the need for padding. FINDSTR is much faster than FOR /F at removing the dynamic portion. Dynamic lines can be safely be prefixed with a semicolon (including labels!). Then the FINDSTR /V option is used to exclude lines that begin with a semicolon and the new dynamic code can simply be appended.
#echo off
setlocal DisableDelayedExpansion
echo The starting filesize is %~z0
:loop
echo ----------------------
call :changeBatch
call :dynamicCode1
call :dynamicCode2
echo The current value=%value%
set /p "quit=Enter Q to quit, anything else to continue: "
if /i "%quit%"=="Q" exit /b
goto :loop
:changeBatch
(
findstr /v "^;" "%~f0"
setlocal enableDelayedExpansion
set /a newValue=value+1, extra=!random!%%9
echo ;:dynamicCode1
echo ;set value=!newValue!
echo ;exit /b
echo ;
echo ;:dynamicCode2
for /l %%n in (1 1 !extra!) do echo ;echo extra line %%n
echo ;exit /b
endlocal
) >"%~f0.tmp"
move /y "%~f0.tmp" "%~f0" > nul
echo The new filesize is %~z0
exit /b
;:dynamicCode1
;set value=33
;exit /b
;
;:dynamicCode2
;echo extra line 1
;exit /b
Short answer: yes, batch files can modify themselves whilst running. As others have already confirmed.
Years and years ago, back before Windows 3, the place I worked had an inhouse menu system in MS-DOS. The way it ran things was quite elegant: it actually ran from a batch file that the main program (written in C) modified in order to run scripts. This trick meant that the menu program itself was not taking up memory space whilst selections were running. And this included things like the LAN Mail program and the 3270 terminal program.
But running from a self-modifying batch file meant its scripts could also do things like load TSR programs and in fact could do pretty much anything you could put in a batch file. Which made it very powerful. Only the GOTO command didn't work, until the author eventually figured out how to make the batch file restart itself for each command.
Nearly like rein said, cmd.exe remember the file position (not only the line position) it's currently is, and also for each call it push the file position on an invisble stack.
That means, you can edit your file while it's running behind and before the actual file position, you only need to know what you do ...
A small sample of an self modifying batch
It changes the line set value=1000 continuously
#echo off
setlocal DisableDelayedExpansion
:loop
REM **** the next line will be changed
set value=1000
rem ***
echo ----------------------
echo The current value=%value%
<nul set /p ".=Press a key"
pause > nul
echo(
(
call :changeBatch
rem This should be here and it should be long
)
rem ** It is neccessary, that this is also here!
goto :loop
rem ...
:changeBatch
set /a n=0
set /a newValue=value+1
set /a toggle=value %% 2
set "theNewLine=set value=%newValue%"
if %toggle%==0 (
set "theNewLine=%theNewLine% & rem This adds 50 byte to the filesize.........."
)
del "%~f0.tmp" 2> nul
for /F "usebackq delims=" %%a in ("%~f0") DO (
set /a n+=1
set "line=%%a"
setlocal EnableDelayedExpansion
if !n!==5 (
(echo !theNewLine!)
) ELSE (
(echo !line!)
)
endlocal
) >> "%~f0.tmp"
(
rem the copy should be done in a parenthesis block
copy "%~f0.tmp" "%~f0" > nul
if Armageddon==TheEndOfDays (
echo This can't never be true, or is it?
)
)
echo The first line after the replace action....
echo The second line comes always after the first line?
echo The current filesize is now %~z0
goto :eof
The command interpreter appears to remember the byte offset within each command file it is reading, but the file itself is not locked, so it is possible to make changes, say with a text editor, whilst it is running.
If a change is made to the file after this remembered location, the interpreter should happily continue to execute the now modified script. However if the change is made before that point, and that modification changes the length of the text at that point (for example you've inserted or removed some text), that remembered location is now no longer referring to the start of that next command. When the interpreter tries to read the next 'line' it will instead pick up a different line, or possibly part of a line depending on how much text was inserted or removed. If you're lucky, it will probably not be able to process whatever word it happen to land on, give an error and continue to execute from the next line - but still probably not what you want.
However, with understanding of what's going on, you can structure your scripts to reduce the risk. I have scripts that implement a simply menu system, by displaying a menu, accepting input from the user using the choice command and then processing the selection. The trick is to ensure that the point where the script waits for input is near the top of the file, so that any edits you might wish to make will occur after that point and so have no nasty impacts.
Example:
:top
call :displayMenu
:prompt
REM The script will spend most of its time waiting here.
choice /C:1234 /N "Enter selection: "
if ERRORLEVEL == 4 goto DoOption4
if ERRORLEVEL == 3 goto DoOption3
if ERRORLEVEL == 2 goto DoOption2
goto DoOption1
:displayMenu
(many lines to display menu)
goto prompt
:DoOption1
(many lines to do Option 1)
goto top
:DoOption2
(many lines to do Option 2)
goto top
(etc)

Resources