So I have a for loop that does an iteration of a SQL stored procedure for every line in a file queue.txt
, now that all works great, what DOESNT however is that if i
You put in your question "if another line is added to the bottom of the file..."; however, your code does not add a line, but completely replaces the entire file contents (although the new contents just have one new line added):
FOR /f "delims=" %%a in ('type queue.txt') DO (
IF NOT EXIST reset.sql (
. . .
type queue.txt | findstr /v %%a> new.txt
rem Next line REPLACES the entire queue.txt file!
type new.txt> queue.txt
echo New list of laptops waiting:>> log.txt
. . .
if exist reset.sql del /f /q reset.sql
)
)
You may change the method to process queue.txt file by redirecting it into a subroutine that read its lines via SET /P command and a loop assembled with GOTO. This way, the lines that be added to the bottom of queue.txt file inside the read loop will be immediately read when the read process reaches they.
call :ProcessQueue < queue.txt >> queue.txt
goto :EOF
:ProcessQueue
set line=
rem Next command read a line from queue.txt file:
set /P line=
if not defined line goto endProcessQueue
rem In following code use %line% instead of %%a
IF NOT EXIST reset.sql (
. . .
type queue.txt | findstr /v %%a> new.txt
rem Next command ADD new lines to queue.txt file:
type new.txt
echo New list of laptops waiting:>> log.txt
. . .
if exist reset.sql del /f /q reset.sql
)
goto ProcessQueue
:endProcessQueue
exit /B
Of course, if the new lines are added by other processes the new lines will be read and processed by this Batch file automatically.
You must be aware that this method ends at the first empty line in queue.txt file; it also have some restrictions in the characters that it can process.
EDIT: This is a simple example that show how this method work:
set i=0
call :ProcessQueue < queue.txt >> queue.txt
goto :EOF
:ProcessQueue
set line=
set /P line=
if not defined line goto endProcessQueue
echo Line processed: %line% > CON
set /A i=i+1
if %i% == 1 echo First line added to queue.txt
if %i% == 2 echo Second line added to queue.txt
goto ProcessQueue
:endProcessQueue
exit /B
This is queue.txt file at input:
Original first line
Original second line
Original third line
Original fourth line
This is the result:
Line processed: Original first line
Line processed: Original second line
Line processed: Original third line
Line processed: Original fourth line
Line processed: First line added to queue.txt
Line processed: Second line added to queue.txt