|
Answer» Finally, I found a forum for DOS. I hope you guys can help me. I've never taken any shot at batch script, so I hope you can help me with this simple task.
Basically, as the subject suggests, what I'm trying to do is check if a service is running or not. And then it will show me the STATUS and give me the appropriate options. If it is running, stop it and vice versa.
I figured out the dos command, which is following
Code: [Select]sc query serviceName | find /I "state" It outputs a line SAYING whether the service is running or not. Now, I'm trying to use this command in a batch script. How do I store the output in a variable, examine the variable and if the service is running/not, then show the appropriate option.
I started with the following, but it doesn't seem to work. Code: [Select]@echo off set test= sc query serviceName | find /I "state" echo %test% pause Could you please advise. If there's a better way, please let me know. Your help is much APPRECIATED. Thanks
in DOS, most of the time , you would have to use a for loop. Set up the for loop to get the variable see for /? for more info.Are we really talking about DOS here, or is it Win2k/XP/Vista command prompt? (I suspect the latter because of the mention of "services".) Come on folks, get it right.
If the latter you can use the && (success) and || (failure) test operators
In a statement with &&, the command on the left of the operator is carried out, and if it was successful, the command on the right is executed. (E.g. with FIND, success=the text specified was found.)
In a statement with || the opposite is the CASE, the command on the right is executed if the ONE on the left fails.
Some examples...
I send the FIND output to nul to avoid screen clutter.
Code: [Select]C:\>sc query winmgmt | find /I "running">nul && echo winmgmt is running winmgmt is running
C:\>sc query nosuchname | find /I "running">nul || echo nosuchname is NOT running nosuchname is NOT running
Code: [Select]set servicename=winmgmt set message=NO sc query %servicename% | find /I "running">nul && set message=YES
Also, since the key word RUNNING is always in upper case you could save 3 characters by ditching the /i switch & specifyiing the string in all caps.
Hey Dias,
Thanks for the corrections. You are right. I was talking about command prompt. Thanks for the examples as well. I'll go from here and see how far I can get. Thanks again.
|