Jump to content











Photo
- - - - -

Integrating WMIC into batch scripting


  • Please log in to reply
3 replies to this topic

#1 spleenharvester

spleenharvester

    Member

  • Members
  • 91 posts
  •  
    United Kingdom

Posted 14 May 2016 - 01:22 AM

Say if I have a command like:

 

"wmic path win32_battery get batterystatus"

 

Is there any easy way of setting the returned number into a variable, or do I need to stick to the old fashioned method of outputting to .txt with FOR loops?



#2 Wonko the Sane

Wonko the Sane

    The Finder

  • Advanced user
  • 13572 posts
  • Location:The Outside of the Asylum (gate is closed)
  •  
    Italy

Posted 14 May 2016 - 08:18 AM

There is no need to output to a (temporary) text file, but you need a FOR /F loop.

 

It also depends if the query returns a single value or several ones.

 

Examples (for single value returned):

FOR /F "tokens=* delims=" %%A IN ('wmic path win32_battery get batterystatus /format:list') DO SET my_%%A

or

FOR /F "tokens=1,2 delims==" %%A IN ('wmic path win32_battery get batterystatus /format:list') DO SET my_%%A=%%B

 

:duff:

Wonko


  • spleenharvester likes this

#3 spleenharvester

spleenharvester

    Member

  • Members
  • 91 posts
  •  
    United Kingdom

Posted 14 May 2016 - 03:03 PM

There is no need to output to a (temporary) text file, but you need a FOR /F loop.

 

It also depends if the query returns a single value or several ones.

 

Examples (for single value returned):

FOR /F "tokens=* delims=" %%A IN ('wmic path win32_battery get batterystatus /format:list') DO SET my_%%A

or

FOR /F "tokens=1,2 delims==" %%A IN ('wmic path win32_battery get batterystatus /format:list') DO SET my_%%A=%%B

 

:duff:

Wonko

 

Ooh neat, I did not know about the /format:list parameter, that has simplified things a lot! Thankyou!



#4 Wonko the Sane

Wonko the Sane

    The Finder

  • Advanced user
  • 13572 posts
  • Location:The Outside of the Asylum (gate is closed)
  •  
    Italy

Posted 15 May 2016 - 09:00 AM

You are welcome. :)

Just in case a slightly more complex example for "pairs" of values:

@ECHO OFF
SETLOCAL ENABLEEXTENSIONS

FOR /F "tokens=1,2 delims==" %%A IN ('wmic path win32_logicaldisk get name^,volumeserialnumber /format:list ^|FIND "="') DO (
CALL :do_parse %%A %%B
)
SET Volume

GOTO :EOF

:do_parse
IF %1==Name SET Varname=Volume_%2
IF %1==VolumeSerialNumber SET %Varname%=%2
IF NOT DEFINED %VarName% SET %Varname%=No_Serial
GOTO :EOF

:duff:

Wonko






0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users