Help with a batch file and if statements

May 21, 2009 at 09:27:55
Specs: Windows XP
How can I use the output of
sc query "service" | find /I "STATE
to set a variable in an if statement?

I basically want it to do this

sc query "service" | find /I "STATE
if service state == 4 go to running
if service state < 4 go to stopped

msg * stopped

msg * running

See More: Help with a batch file and if statements

Report •

May 21, 2009 at 13:56:22
I answered my own question eventually, in case someone else finds this in a search, it is:

d:\temp\Sc.exe query "[service name]" |d:\temp\Find.exe "STOPPED"' & @crlf & _
echo. %ERRORLEVEL%' & @crlf & _
If %ERRORLEVEL% EQU 0 (' & @crlf & _
echo. GOTO run' & @crlf & _
) Else (' & @crlf & _
echo. The notification service could not be stopped. >> [log file location - not a necessity]' & @crlf & _
echo. >> [log file location - not a necessity]' & @crlf & _
\\server path to executable to run - not necessary]' & @crlf & _
GOTO :END' & @crlf & _
)' & @crlf & _
:run' & @crlf & _

Report •

May 21, 2009 at 16:45:18
I know you have already solved your problem but thought you
still might be interested in this:

set service=spooler
for /f "tokens=3" %%a in ('sc query "%service%"^|find "STATE"') do (
	if %%a==4 goto running
	goto stopped
msg * stopped
goto :eof
msg * running

Report •

May 21, 2009 at 20:19:30
This may be a stupid question, but can you elaborate on this part of it word by word and what each part does?

for /f "tokens=3" %%a in ('sc query "%service%"^|find "STATE"') do (
if %%a==4 goto running

Report •

Related Solutions

May 21, 2009 at 20:44:47
for /f - A loop that iterates over lines of text that can break the lines up into "chunks"(tokens).

"tokens=3" - Option telling the for loop that we only want the third chunk of text. By default the delimiters are tabs or spaces, but that can also be changed by using the "delims=" option.

%%a - Variable that the for loop will use, can be any letter and some other characters, it is case sensitive.

in ('sc query "%service%"^|find "STATE"') - What the for loop will run over, it can either be a file, command or string. In this case it is a command. The method of quoting tells the for loop what it is processing, single quotes mean that it is a command unless the usebackq option is present, which changes the semantics. Note that special characters like "|" are prefixed with a caret ^ so their meaning isn't interpreted until the command is executed.

do ( Everything in the code block (or after do if it is a single line) is processed once every line where the variable is swapped for the text from that line.

For more detailed info start>run>cmd>"for /?"(without quotes)

Report •

May 22, 2009 at 05:20:33
Hi there,

I thought you couldn't use GOTO in a FOR loop. (or at least I was told to steer clear from them. I must have been mistaken and could have used that a long time ago!!!)

What is "@crlf"


Report •

May 22, 2009 at 06:08:23
You can use goto in a for loop but it will break the loop so it will stop iterating, with the exception of for /l loop where it will keep iterating but do nothing.

Unless you are intentionally breaking the loop it's best to call a label.

Report •

Ask Question