Jump to content

FOR /F with SC in a batch file and Windows XP


Recommended Posts

Hello,
I have a problem running a batch file I'm creating for Windows XP. At some stage it has to check whether a service is running or not. Let's assume that I type in the following command directly into a command prompt:

for /f "tokens=3" %H in ('sc query Spooler ^| findstr "STATE"') do echo %H

It runs flawlessly and outputs 1 or 4, depending on a service state. The problem starts when I put this command in a batch file, suitably modified by doubling % signs:

for /f "tokens=3" %%H in ('sc query Spooler ^| findstr "STATE"') do echo %%H

or any other for /f loop where sc is a command to process - even sc alone in a loop. When processing the command, cmd.exe processes start to multiply indefinitely and the batch execution hangs. I've tried both XP HE and Pro, English and Polish variants. Also, there is no such problem under Windows 8. For what I've checked, this issue only applies to sc - any other command I've tried in a loop ran without a problem. This command alone:

sc query Spooler ^| findstr "STATE"

also runs without a hassle. Has anyone encountered such a behavior? Any ideas?
Regards.

Edited by darqious
Link to comment
Share on other sites


It works here (the batch I mean) just like the command line version, (XP PRO SP2 Italian), it must be something specifically on your machines, but really cannot say what it could be.

The output of the simple command here is this:
 

Quote

C:\batches>sc query Spooler

SERVICE_NAME: Spooler
        TYPE               : 110  WIN32_OWN_PROCESS (interactive)
        STATE              : 4  RUNNING
                                (STOPPABLE,NOT_PAUSABLE,ACCEPTS_SHUTDOWN)
        WIN32_EXIT_CODE    : 0  (0x0)
        SERVICE_EXIT_CODE  : 0  (0x0)
        CHECKPOINT         : 0x0
        WAIT_HINT          : 0x0

Try this (but I don't think that it will  change anything:
 

Quote

FOR /F "tokens=* delims=" %%H IN ('sc query Spooler ^| find "STATE"') DO CALL :do_echo %%H
GOTO :EOF

:do_echo
ECHO %*
ECHO %3

jaclaz
 

Link to comment
Share on other sites

Uhm...
After struggling with it for the last 3 days it's a shame to tell, but... for testing purposes I had to give my batch file any name, and my first thought was... sc.cmd
SC.freaking.cmd
And I named it like that every time I tested it on XP.

...please don't. I'll die by myself.

Problem solved.
Thanks and regards.

Edited by darqious
Link to comment
Share on other sites

8 hours ago, darqious said:

Uhm...
After struggling with it for the last 3 days it's a shame to tell, but... for testing purposes I had to give my batch file any name, and my first thought was... sc.cmd
SC.freaking.cmd
And I named it like that every time I tested it on XP.

...please don't. I'll die by myself.

Problem solved.
Thanks and regards.

Nice one, I would have never thought of that, something to remember, particularly when one is tired and/or hits a roadblock and insists on it head down, without taking some time out for a walk ...

Haopy everything is well now.

jaclaz 
 

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...