-1
for %%f in ('wmic service where 'name like 'Steam Client%%'") do set Service=%%f
echo %Service%

What am I doing wrong?

Pang
  • 9,564
  • 146
  • 81
  • 122
Ramesh Eol
  • 41
  • 8

3 Answers3

2
for /F "tokens=*" %%f in ('wmic service where "name like "Steam%%"" get Name ^| findstr /V /R "^$"') do set Service=%%f
echo %SERVICE%

Output

Steam Client Service

Mistakes in your code:

  • Did not use /F switch with for-loop
  • Used single and double-quotes intermixed.
  • Did not specify a specific property to get with the get clause
  • Did not filter extra output with findstr
abelenky
  • 63,815
  • 23
  • 109
  • 159
  • This out put "Steam Client Service" is giving me trailing spaces. Like "Steam Client Service (spaces)" – Ramesh Eol Mar 05 '17 at 08:36
  • http://stackoverflow.com/questions/3001999/how-to-remove-trailing-and-leading-whitespace-for-user-provided-input-in-a-batch – abelenky Mar 05 '17 at 14:01
0

Isn't the last bracket in ('wmic service where 'name like 'Steam Client%%'") supposed to be ' not " ? In any case, I have never tried to use wmic service, only wmic win32_localtime which looked like this: FOR /F %%F IN ('wmic path win32_localtime get dayofweek^|Findstr [0-6]') DO SET DOW=%%F

EDIT: I notice several places where you have gone wrong here, at least in the parts that I can understand. If you want the output of the code to be evaluated by a FOR statement you require /F infront of FOR and before the variable. You also normally need to start and end the brackets containing the command with a single quote ('). Like so: for /f %%f in ('wmic service where name like "Steam Client%%"') do set Service=%%f echo %Service%

For any assistance with WMIC queries this might help

Hope this helps.

BasiliskHill
  • 64
  • 1
  • 9
  • I want the batch file to find the name of the service and display it on screen. Thank You for your quick response. – Ramesh Eol Mar 05 '17 at 07:29
  • Did you note the first relevant part of this answer? Where you are supposed to have the command within the brackets needs to be ended (and started) with a single quote ('). and then the second part, where I showed what I had done with a for command to use a command. You require /F in front of FOR if you are using a command in the statement. – BasiliskHill Mar 05 '17 at 07:39
0

This is how I'd do it.

The second for loop strips some problematic characters from the output, much in the same way as the FindStr example, only quicker. I then implement a Call to strip the trailing spaces which pad the end of the returned Get string.

@Echo Off
For /F "Skip=1 Delims=" %%A In (
    '"WMIC Service Where (Name Like 'Steam%%') Get Name"'
) Do For /F "Delims=" %%B In ("%%A") Do Call :Sub %%B
Echo("%Service%"
Timeout -1
GoTo :EOF

:Sub
Set "Service=%*"
GoTo :EOF

The above code is completely untested as I'm using an android device.

Compo
  • 36,585
  • 5
  • 27
  • 39