Search code examples
batch-filecmdwmic

Remove unwanted spaces in wmic results through batch file


Im working on a batch file in which I need to show the estimated charge remaining of the battery, I have achieved my goal but sadly I am not happy with the results due to excesive and unwanted spaces in the command result.

Here's my code:

:charge
FOR /F "delims= skip=1" %%i IN ('WMIC PATH Win32_Battery Get EstimatedChargeRemaining') DO ( SET CHR=%%i
GOTO results )

:results
ECHO "Battery Percentage: %CHR%"
PAUSE
GOTO menu

The result of the above command is: "Battery Percentage: 100 "

How do I get rid of those spaces at the end of the result?

Thanks in advance and any kind of help is greatly appreciated.


Solution

  • try like this:

    :charge
    @echo off
    FOR /F "tokens=* delims=" %%i IN ('WMIC PATH Win32_Battery Get EstimatedChargeRemaining /format:value') DO (
        for /f "tokens=* delims=" %%# in ("%%i") do set "%%#"
    )
    echo %EstimatedChargeRemaining%
    

    WMIC adds additional character but it can be removed with one more for loop -> https://www.dostips.com/forum/viewtopic.php?t=4266