A1229
A1229

Reputation: 11

unable to get arithmetic calculation answer in Batch script

I am trying to get Percentage Memory usage of a particular process PID, but only values are getting printed and not the calculation output value.

@echo off

setlocal enableextensions EnableDelayedExpansion

set PID=5716

for /f "tokens=5 delims= " %%F in ('tasklist /nh /FI "PID eq !PID!"') do (
   for /f "tokens=2 delims==" %%a in ('wmic computersystem get TotalPhysicalMemory /value')  do (
          set "MEM=((%%F*100) /%%a)"

    )
    )
    )
echo PercentageMemory = %MEM%

I am new to batch scripting. Am I missing something?

Upvotes: 0

Views: 99

Answers (1)

Compo
Compo

Reputation: 38664

Based upon the advice I provided in my comment, and using a different method of determining the process associated with the PID, you could do it like this, (change the PID, 5716, on line 2 as necessary):

@SetLocal EnableExtensions
@Set "PID=5716"
@Set "$Mem="
@For /F "Tokens=2 EOL=N Delims=," %%G In (
    '%__AppDir__%wbem\WMIC.exe OS Get TotalVirtualMemorySize^,Version^
     /Format:CSV 2^>NUL')Do @For /F "Tokens=2 EOL=N Delims=," %%H In (
    '%__AppDir__%wbem\WMIC.exe Path Win32_PerfFormattedData_PerfProc_Process^
     Where "IDProcess='%PID%'" Get WorkingSet^,WorkingSetPrivate /Format:CSV^
     2^>NUL')Do @Set /A "$Mem=%%H/1024*100/%%G">NUL 2>&1
@If Defined $Mem Echo %$Mem%%%&Pause

Please note however, that if the arithmetic meets numbers which exceed the Set /A limitation of 32-bit signed integers, the method will fail. Also, you should note that because we're working with integers, the result will always be rounded down to the nearest integer. This means anything below 1% will show as 0%

If I remember correctly, there is a delay when you use this method for the first time in any session, after that, you'll usually notice a significant speed increase.


If you already know the name of the process, which is usually the name of the executable without its extension, then you could use that instead of the PID, (change the ExecutableFile string on line 2 to your process name as necessary):

@SetLocal EnableExtensions
@Set "MyProcess=ExecutableFile"
@Set "$Mem="
@For /F "Tokens=2 EOL=N Delims=," %%G In (
    '%__AppDir__%wbem\WMIC.exe OS Get TotalVirtualMemorySize^,Version^
     /Format:CSV 2^>NUL')Do @For /F "Tokens=2 EOL=N Delims=," %%H In (
    '%__AppDir__%wbem\WMIC.exe Path Win32_PerfFormattedData_PerfProc_Process^
     Where "Name='%MyProcess%'" Get WorkingSet^,WorkingSetPrivate /Format:CSV^
     2^>NUL')Do @Set /A "$Mem=%%H/1024*100/%%G">NUL 2>&1
@If Defined $Mem Echo %$Mem%%%&Pause

Upvotes: 0

Related Questions