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?
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?
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
Wonko
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
Wonko
Ooh neat, I did not know about the /format:list parameter, that has simplified things a lot! Thankyou!
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
Wonko
0 members, 0 guests, 0 anonymous users