Search code examples
windowsbatch-filewmic

Windows Batch wmic OS get FreePhysicalMemory


i try to write a batch file which gives me the free physical memory. This batch should write every X seconds a timestamp and the following command in a text file:

wmic OS get FreePhysicalMemory

My batch file looks like this:

:loop
(
@echo %time% %date%  
wmic OS get FreePhysicalMemory /Value
)>testMEMORY.txt
timeout /t 10 
goto loop

This is the output:

13:54:54,76 04.11.2019  




 F r e e P h y s i c a l M e m o r y = 7 2 0 6 6 4 8 

I don't get it, why i have a lot of new lines and after each character a space. Can anybody help me please?


Solution

  • The output of WMIC is unicode !

    The trailing <CR> can be removed by passing the value through another FOR /F loop.

    This also removes the phantom "blank" line (actually a <CR>)

    @echo off
    If Exist testMEMORY.txt Del testMEMORY.txt
    :loop
    (
        @echo %time% %date%  
        for /f "skip=1 delims=" %%a in ('wmic OS get FreePhysicalMemory /Value') do (
            for /f "delims=" %%b in ("%%a") do (
                echo %%b
            )
        )
    )>>testMEMORY.txt
    timeout /t 10 /nobreak>nul 
    goto loop
    

    So, i got as result in testMEMORY.txt something like that :

    15:32:19.02 04/11/2019  
    FreePhysicalMemory=528592
    15:32:29.19 04/11/2019  
    FreePhysicalMemory=530804
    15:32:39.14 04/11/2019  
    FreePhysicalMemory=531156
    15:32:49.21 04/11/2019  
    FreePhysicalMemory=530968
    15:32:59.16 04/11/2019  
    FreePhysicalMemory=531936
    15:33:09.23 04/11/2019  
    FreePhysicalMemory=530376
    15:33:19.18 04/11/2019  
    FreePhysicalMemory=524980
    15:33:29.16 04/11/2019  
    FreePhysicalMemory=521816
    15:33:39.21 04/11/2019  
    FreePhysicalMemory=465820
    15:33:49.17 04/11/2019  
    FreePhysicalMemory=461312
    15:33:59.17 04/11/2019  
    FreePhysicalMemory=385172
    15:34:14.21 04/11/2019  
    FreePhysicalMemory=529800
    15:34:24.13 04/11/2019  
    FreePhysicalMemory=541064