1

This works as it should

set "status="
for /F "usebackq skip=1 tokens=3 delims=," %%H in (`schtasks.exe /fo csv /query /tn "\MS2\Import Process"`) do set "status=%%H"
set status=%status:"=%
echo The Import status: %status%

This says the status is READY

I wanted to check the status every 5 seconds for 1 minute:

for /l %%i in (1, 5, 300) do (
    set "status="
    for /F "usebackq skip=1 tokens=3 delims=," %%H in (`schtasks.exe /fo csv /query /tn "\MS2\Import Process"`) do set "status=%%H"
    set status=%status:"=%
    echo The Import status: %status%
    timeout /t 5
)

This says the status is always empty

I am using %% notion because I run it in a batch file.

Why doesnt status get set properly in the for loop

Edit: Attempt with delayedexpansion

setlocal EnableDelayedExpansion

for /l %%i in (1, 5, 300) do (
    set "status="
    for /F "usebackq skip=1 tokens=3 delims=," %%H in (`schtasks.exe /fo csv /query /tn "\MS2\Import Process"`) do set "status=%%H"
    echo The Import status: !status!
    timeout /t 5
)

It still just says status is empty. I also get ERROR: The system cannot find the file specified. just before the echo

I tried putting set "status=" outside the for loop with no avail.

LearningJrDev
  • 911
  • 2
  • 8
  • 27
  • ...the famous delayed expansion trap -- see [this site](http://ss64.com/nt/delayedexpansion.html) (there is a great example with a `for /L` loop)... – aschipfl May 17 '16 at 20:25
  • Not having much luck using it, I don't know if `%%H` becomes `!!H` the whole thing is confusing. – LearningJrDev May 17 '16 at 20:31
  • Keep `%%H`. Change `%status%` to `!status!` and `%status:"=%` to `!status:"=!` etc. – JosefZ May 17 '16 at 20:45
  • That is what I originally thought but it says the status is now `"=` rather than `READY`. if I remove the `set status=!status:"=!` I just get a blank status just like before – LearningJrDev May 17 '16 at 20:48
  • Before you actually use `delayed expansion` (that is `!status!`), you need to enable it by placing `setlocal EnableDelayedExpansion` on top of your script, because it is disabled as per default... – aschipfl May 17 '16 at 20:49
  • Yeah, I added `setlocal EnableDelayedExpansion`. Ill update attempt in question – LearningJrDev May 17 '16 at 20:50
  • [Remove surrounding `"` double quotes](http://ss64.com/nt/syntax-args.html) using `%%~H` and skip trailing blank line taken from `schtasks` output using `if not defined status set "status=%%~H"`. – JosefZ May 17 '16 at 20:56
  • Please post as an answer because I am getting confused. – LearningJrDev May 17 '16 at 21:00

1 Answers1

1
  • Remove "surrounding double quotes" using %%~H and
  • skip trailing blank line taken from schtasks output using if not defined status …
  • Edit: Windows stores scheduled tasks as XML files. To avoid (a bit confusing) message ERROR: The system cannot find the file specified check the task existence (test errorlevel from filesystem).

Updated code snippet:

echo OFF
SETLOCAL EnableExtensions EnableDelayedExpansion
set "tasktocheck=\MS2\Import Process"

rem check scheduled task existence silently
>NUL 2>&1 schtasks.exe /fo csv /query /tn "%tasktocheck%"
if errorlevel 1 (
     echo "%tasktocheck%" scheduled task not found
     rem quit the script raising errorlevel 1
     exit /B 1
)
for /l %%i in (1, 5, 300) do (
    rem remove variable `status` (i.e. make it undefined) in next line
    set "status="
    for /F "usebackq skip=1 tokens=3 delims=," %%H in (
            `schtasks.exe /fo csv /query /tn "%tasktocheck%"`
        ) do if not defined status set "status=%%~H"
    rem      ^^^^^^^^^^^^^^^^^^^^^  skip trailing blank line taken from schtasks output
    rem Remove "surrounding double quotes"       ^  note the ~ tilde
    echo The Import status: !status!
    timeout /t 5
)

Resources (required reading):

Community
  • 1
  • 1
JosefZ
  • 28,460
  • 5
  • 44
  • 83
  • Ok that is what I thought you meant for the most part. I am getting `ERROR: The system cannot find the file specified.` `The Import status:` every time the loop runs. So still the same issues – LearningJrDev May 17 '16 at 22:12
  • @LearningJrDev answer updated. That `The system cannot find the file specified` is a bit confusing message isn't it? – JosefZ May 17 '16 at 22:33