I have the need to run a .bat file to run a SQL command on demand, It needs to have an if nested within a for and the if should repeat until true.
What I have:
@echo off
cd "%UserProfile%\Desktop\Scripting\"
FOR /f "delims=" %%a in ('type queue.txt') DO (
:loop
IF EXIST reset.sql (
goto loop
) ELSE (
::Create SQL command
echo USE dbname> reset.sql
echo EXEC dbo.sp_ResetSubscription @ClientName = '%%a'>> reset.sql
echo EXEC dbo.sp_RunClientSnapshot @ClientName = '%%a'>> reset.sql
sqlcmd -i "reset.sql"
if exist reset.sql del /f /q reset.sql
)
)
if exist queue.txt del /f /q queue.txt
This bombs out when it hits the loop, if I move :loop from where it is to within the if statement it works fine, however that isn’t much use.
What I need it to do is to keep looping until reset.sql does not exist but at the same time stay within the same iteration of the loop.
You should not ever GOTO a :label within a parenthesized block of code. See https://stackoverflow.com/a/8481978/1012053 – it deals with an IF() block, but the concept is the same for a FOR..DO() block. Performing a GOTO within a FOR..DO() will abort the remainder of the FOR iterations NOTE – a FOR /L loop will silently finish counting without actually processing the DO() clause
This modification should give you the behavior you are looking for.