Start batch file, then loop to check if it's done yet

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
bollwerk
Posts: 3
Joined: 30 Oct 2015 10:59

Start batch file, then loop to check if it's done yet

#1 Post by bollwerk » 30 Oct 2015 11:16

Not sure what I'm doing wrong here.
From all the stuff I've found googling, this should work, but it doesn't.
I'm sure someone here can show me what I've done wrong.

This is a test to make sure my code works, before I implement the same concepts in production.
infinite_test.bat is just a test batch file that echos a text line, waits 10 sec, then loops to the beginning.
In theory, my code here is supposed to find that it's still running and loop back to keep checking, but in actuality the code just checks once, then goes to :end. I don't understand why errorlevel is being set to 0, when "find" is actually finding the running task window.

Thanks in advance for any help.

Code: Select all

@echo off
start "Infinite test" infinite_test.bat
:loop
timeout /t 10 >nul
tasklist /fi "imagename eq cmd.exe" /v | find /i "Infinite"
if not errorlevel 1 goto end

echo %errorlevel% - waiting for window to close
goto loop

:end
echo %errorlevel% - batch is done

Squashman
Expert
Posts: 4486
Joined: 23 Dec 2011 13:59

Re: Start batch file, then loop to check if it's done yet

#2 Post by Squashman » 30 Oct 2015 11:19

Code: Select all

if not errorlevel 1 goto end

What do you think this means?

bollwerk
Posts: 3
Joined: 30 Oct 2015 10:59

Re: Start batch file, then loop to check if it's done yet

#3 Post by bollwerk » 30 Oct 2015 12:09

My understanding is that the "find" command will return an errorlevel of 0 for "not found" and 1 for "found".
So that line is supposed to see if the errorlevel is something other than 1 and if so, jump to :end, since that should mean that the process I'm trying to find is not running. I tried changing the line to:

Code: Select all

if errorlevel 0 goto end

but the undesired behavior remained.

Squashman
Expert
Posts: 4486
Joined: 23 Dec 2011 13:59

Re: Start batch file, then loop to check if it's done yet

#4 Post by Squashman » 30 Oct 2015 12:11

bollwerk wrote:My understanding is that the "find" command will return an errorlevel of 0 for "not found" and 1 for "found".

Real easy way to test that theory now isn't there. You may be surprised by the results.

Squashman
Expert
Posts: 4486
Joined: 23 Dec 2011 13:59

Re: Start batch file, then loop to check if it's done yet

#5 Post by Squashman » 30 Oct 2015 12:21

Squashman wrote:

Code: Select all

if not errorlevel 1 goto end

What do you think this means?


This really means IF ERRORLEVEL is NOT greater than or equal to 1

bollwerk
Posts: 3
Joined: 30 Oct 2015 10:59

Re: Start batch file, then loop to check if it's done yet

#6 Post by bollwerk » 30 Oct 2015 16:06

OK, I removed the line

Code: Select all

if not errorlevel 1 goto end

and just let it run.
It looks like when find finds a match, the errorlevel is set to 0, not 1.
And when a match is NOT found, it returns a 1.
So it appears the information I found on another forum was backwards, or more likely I misread or misunderstood it.

Thanks for the help!

Squashman
Expert
Posts: 4486
Joined: 23 Dec 2011 13:59

Re: Start batch file, then loop to check if it's done yet

#7 Post by Squashman » 30 Oct 2015 16:11

Correct. Errorlevel is set to 0 when the find command is successful. You could have tested that theory real easy at the cmd prompt.

Code: Select all

C:\>echo findme |find "me"
C:\>echo %errorlevel%

Post Reply