1

I am trying to get highest memory consuming process id in batch file. this so far I reached but it not working.

@echo off
set old=0
for /f "TOKENS=1" %%a in ('wmic PROCESS where "Name='cmd.exe'" get WorkingSetSize ^| findstr [0-9]') do if %%a GTR %old% (set old=%%a)
echo %old%
3
  • 1
    You need Setlocal ENABLEDELAYEDEXPANSION and replace %old% with !old!. See set /? for the explanation. Commented Oct 23, 2013 at 12:58
  • Just for the record, you can only compare up to 2 GB using plain batch Commented Oct 24, 2013 at 0:20
  • 1
    Thanks Butter, Foxdrive: maximum 2GB, It means batch variable cant hold numeric value more than 2*1024*1024*8 values. Is it? Commented Oct 25, 2013 at 5:25

1 Answer 1

1

This should work...

@echo off 
Setlocal ENABLEDELAYEDEXPANSION
set old=0 
for /f "TOKENS=1" %%a in ('wmic PROCESS where "Name='cmd.exe'" get WorkingSetSize ^| findstr [0-9]') do (
  if %%a GTR !old! (
    set old=%%a
  ) 
 echo !old!
)

Set /? explains delayed environment variable expansion...

Delayed environment variable expansion is useful for getting around
the limitations of the current expansion which happens when a line
of text is read, not when it is executed.  The following example
demonstrates the problem with immediate variable expansion:

set VAR=before
if "%VAR%" == "before" (
        set VAR=after
        if "%VAR%" == "after" @echo If you see this, it worked
    )

would never display the message, since the %VAR% in BOTH IF statements
is substituted when the first IF statement is read, since it logically
includes the body of the IF, which is a compound statement.  So the
IF inside the compound statement is really comparing "before" with
"after" which will never be equal.  Similarly, the following example
will not work as expected:

    set LIST=
    for %i in (*) do set LIST=%LIST% %i
    echo %LIST%

in that it will NOT build up a list of files in the current directory,
but instead will just set the LIST variable to the last file found.
Again, this is because the %LIST% is expanded just once when the
FOR statement is read, and at that time the LIST variable is empty.
So the actual FOR loop we are executing is:

    for %i in (*) do set LIST= %i

which just keeps setting LIST to the last file found.

Delayed environment variable expansion allows you to use a different
character (the exclamation mark) to expand environment variables at
execution time.  If delayed variable expansion is enabled, the above
examples could be written as follows to work as intended:

    set VAR=before
    if "%VAR%" == "before" (
        set VAR=after
        if "!VAR!" == "after" @echo If you see this, it worked
    )

    set LIST=
    for %i in (*) do set LIST=!LIST! %i
    echo %LIST%
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.