The following seems to work. I changed the single quotes in the second for loop to double quotes and added parentheses for that loop.
schtasks /Query /nh /fo CSV | findstr /i /c:"time" | FOR /f "tokens=1 delims=," %%f IN ('more') DO (
:loop
FOR /f "tokens=*" %%a IN ("schtasks /query /nh /fo CSV /tn %%f ^| findstr /i /c:"running"") DO (
SET task=%%a
IF [%task%] == [] (
schtasks /change /tn %%f /DISABLE
) ELSE (
GOTO loop
)
)
)
Update:
achipfl is right. His answer works for me
I tried again too and came up with the following solution.
First it disables the sheduleing of the task and then end the task.
Then if the task is still running it calls the batch file again. I was not sure if you want to end the task. But because you like to wait for the task to finish execution I have assumed that.
@echo off
set querystring="time"
schtasks /Query /nh /fo CSV | findstr /i /c:"%querystring%" | FOR /f "tokens=1,3 delims=," %%f IN ('more') DO (
if %%g=="running" (
schtasks /change /tn %%f /Disable
schtasks /end /tn %%f
)
)
schtasks /Query /nh /fo CSV | findstr /i /c:"%querystring%" | FOR /f "tokens=1,3 delims=," %%f IN ('more') DO (
if %%g=="running" (
call test2.bat
exit /b 0
)
)
schtasks /Query /nh /fo CSV | findstr /i /c:"%querystring%"
Update 2:
combination
for /F "tokens=1,3 delims=," %%F in ('schtasks /QUERY /NH /FO CSV ^| findstr /I /C:"searchcriteria"') do call :LOOP %%F %%G
goto :EOF
:LOOP
schtasks /CHANGE /TN %1 /DISABLE
if %2=="running" schtasks /END /TN %1 & call :WAIT %1
goto :EOF
:WAIT
FOR /F "tokens=3 delims=," %%F IN ('schtasks /Query /nh /fo CSV /tn %1') DO set "test=%%F"
if %test%=="running" GOTO :WAIT
:EOF
ifas it does not like pipes (|). Perhaps try withif not defined task, orif not^ defined task(^plus two spaces)...forloop in addition (so you have... | (for /F ...)...gotoloop inside of a parenthesised block of code, which cannot work, becausegotobreaks the block context. I would put thegotoloop or even the outerfor /Floop into a sub-routine and usecall. Furthermore, I'd puttimeout 1into thegotoloop in order to avoid heavy processor load...