Home > other >  Use Output of Specific Commands as Variables to Detect if Laptop or Desktop Computer Windows BATCH
Use Output of Specific Commands as Variables to Detect if Laptop or Desktop Computer Windows BATCH

Time:11-12

I have been scratching my head over this one for a while. What I am attempting to do is add auto-detection to my computer diagnostic batch script so I can make scripts specific to laptop or desktop devices that automatically execute according to device type. There are two commands I thought I could use to detect this.

wmic systemenclosure get chassistypes

and

powercfg /batteryreport

Unfortunately I cannot seem to parse the output of either of these commands into a variable that I can actually use. This guide is what I was trying to follow, but I have ran into problems with both wmic and powercfg, wmic outputs cannot be filtered by token as given my example in the linked guide and powercfg just refuses to cooperate. Here is one of the many combinations that hasn't work

for /f %%i in ('powercfg /batteryreport') do set RESULT=%%i

Any help would be much appreciated, I'm hoping its a me problem not a limitation to batch.

CodePudding user response:

If you open cmd and run for /? you will find that there are different functions that you can use with the command, one being delims= which is something you will need for powercfg due to the result containing spaces:

For the wmic one of these will do:

@echo off
for /f "delims={}" %%i in ('wmic systemenclosure get chassistypes ^| findstr "{"') do @set "chassistype=%%i"`
echo %chassistype%

or by using the /VALUE option and setting the variable using the standard output as variable and value:

@echo off
for /f "delims=" %%i in ('wmic systemenclosure get chassistypes /VALUE ^| findstr "{"') do set "%%i"`
echo %chassisTypes%

and powercfg

@echo off
for /f "delims=" %i in ('powercfg /batteryreport') do set "result=%%i"
echo %result%

You can obviously go with all the above commands without having to set the commands by simply echoing them.

  • Related