Editing WMIC output format

12,590

Solution 1

First you would consider to convert the WMIC output encoding from Unicode to ANSI to ommit unicode characters (like empty Echoes), a simple trick to do this is filtering the output with FIND/FINDSTR command(s), after applying the output filter you just need to read the output with FOR /F to split the string using the desired tokens value and delims delimitter.

Here is the code:

@Echo OFF

(For /F "Tokens=1,* Delims==" %%A in (
    'wmic OS get FreePhysicalMemory^,TotalVisibleMemorySize /Format:list ^| FINDSTR "[0-9]"'
)DO (
    Echo %%A= %%B
))>"File.txt"

Pause&Exit

Solution 2

Wmic can be a weird beast. This works here in Windows 8 32 bit.

The extra code is needed to remove the trailing Carriage Return.

@echo off
setlocal enabledelayedexpansion
(For /F "tokens=1,* delims==" %%A in ('"wmic OS get FreePhysicalMemory,TotalVisibleMemorySize /value |find "M" "') do (
set "line=%%A= %%B"
set "line=!line:~0,-1!"
echo !line!
))>output.txt
Share:
12,590
Sunny
Author by

Sunny

Software Engineer #SOreadytohelp

Updated on June 13, 2022

Comments

  • Sunny
    Sunny over 1 year
    wmic OS get FreePhysicalMemory,TotalVisibleMemorySize /Format:Textvaluelist > Output.txt  
    

    Using above command in a batch, I get output as,

    FreePhysicalMemory=1140860
    TotalVisibleMemorySize=2095492
    

    I want to add a single space after equal (=) sign ,sothat it may look like;

    FreePhysicalMemory= 1140860
    TotalVisibleMemorySize= 2095492
    

    I am trying with below but getting error: The syntax of the command is incorrect

    For /F "tokens=1 delims==" %%A in ('wmic OS get FreePhysicalMemory,TotalVisibleMemorySize /Format:Textvaluelist') do 
    echo %%A %%B > Output.txt  
    

    or is there any other way to get the output in required format using batch ?

  • Sunny
    Sunny about 10 years
    @Elektro..its working in cmd..!!.but when i redirect output Echo %%A= %%B > Output.txt...Output.txt file has only TotalVisibleMemorySize= 2095492 not the FreePhysicalMemory .
  • ElektroStudios
    ElektroStudios about 10 years
    Of course the output contains two lines but you are attempting to redirect only one overwritting the textfile, I've edited the code to let you understand how you can redirect both lines. PS: Forgive my English speaking
  • Sunny
    Sunny about 10 years
    @Elektro..Now Its working perfectly like you English..!! :)..Thanks..!
  • foxidrive
    foxidrive about 10 years
    FWIW it still has a trailing CR on each line.
  • Sunny
    Sunny about 10 years
    @foxidrive..Both Yours and Elektro's code are working well on my Windows 7 32 bit without giving any CR..:)
  • foxidrive
    foxidrive about 10 years
    Try using this Echo "%%A= %%B" to check - You may find that the quotes are no longer around the string, in the output file.