For / F + Wmic + WHERE clausule + AND clausule

Just for curiosity, this command:

wmic service where (name="themes" and state="running") get 

How to write along with FOR in a script?

The code below does not work:

 For /F %%a in ( 'wmic service where ^("name='themes'" and "state='running'"^) get' ) do ( echo %%a ) 
+6
source share
3 answers
 @echo off For /F "usebackq delims=" %%a in (`wmic service where 'name^="themes" and state^="running"' get`) do ( echo %%a ) 

this one works for me. I used the usebackq parameter to not have problems with ' and the alternative wmic - ' syntax instead of brackets.

+9
source

You can enclose the full wmic command in single + double quotes, then you do not need to hide anything

 FOR /F "delims=" %%a in ('"wmic service where (name="themes" and state="running") get"') do ( echo %%a ) 
+9
source

Another option :)

 @echo off for /f "delims=" %%A in ( 'wmic service where "name='themes' and state='running'" get' ) do for /f "delims=" %%B in ("%%A") do echo %%B 

Complex WHERE clauses must be either quoted or enclosed in brackets. Additional internal ' does not cause problems with FOR / F.

I added extra FOR / F to cut out the unnecessary carriage return, which is added to the end of each line as an FOR / F artifact that converts WMIC unicode output to ANSII. Without additional FOR / F, there is an additional line consisting solely of a carriage return, which leads to the end of ECHO is off. in the end.

I think I prefer the jeb version because it eliminates the need for escape in the whole command, although I would probably use single quotes in the WHERE clause. For instance:

 @echo off for /f "delims=" %%A in ( '"wmic service where (name='themes' and state='running') get name, pathName"' ) do for /f "delims=" %%B in ("%%A") do echo %%B 

Using syntax in my first code example requires escaping commas in a GET clause:

 @echo off for /f "delims=" %%A in ( 'wmic service where "name='themes' and state='running'" get name^, pathName' ) do for /f "delims=" %%B in ("%%A") do echo %%B 
+9
source

Source: https://habr.com/ru/post/956415/