Computing.Net > Forums > Programming > Help with a batch file and if statements

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

Help with a batch file and if statements

Reply to Message Icon

Name: gte
Date: May 21, 2009 at 09:27:55 Pacific
OS: Windows XP
Subcategory: Batch
Comment:

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

:stopped
msg * stopped

:running
msg * running



Sponsored Link
Ads by Google

Response Number 1
Name: gte
Date: May 21, 2009 at 13:56:22 Pacific
Reply:

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 & _


0

Response Number 2
Name: Judago
Date: May 21, 2009 at 16:45:18 Pacific
Reply:

I know you have already solved your problem but thought you
still might be interested in this:

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


0

Response Number 3
Name: gte
Date: May 21, 2009 at 20:19:30 Pacific
Reply:

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


0

Response Number 4
Name: Judago
Date: May 21, 2009 at 20:44:47 Pacific
Reply:

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)


0

Response Number 5
Name: lee123abc
Date: May 22, 2009 at 05:20:33 Pacific
Reply:

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"

Cheers


0

Related Posts

See More



Response Number 6
Name: Judago
Date: May 22, 2009 at 06:08:23 Pacific
Reply:

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.


0

Sponsored Link
Ads by Google
Reply to Message Icon






Post Locked

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.


Go to Programming Forum Home


Sponsored links

Ads by Google


Results for: Help with a batch file and if statements

Make an exe with a batch file and.. www.computing.net/answers/programming/make-an-exe-with-a-batch-file-and/17595.html

need a batch file www.computing.net/answers/programming/need-a-batch-file/13744.html

Help creating a Batch file urgent www.computing.net/answers/programming/help-creating-a-batch-file-urgent/15963.html