@ECHO OFF
SETLOCAL
FOR /f "delims==" %%i IN ('set $ 2^>nul') DO SET "%%i="
FOR /f "delims=" %%i IN ('wmic process get Name^, ProcessId^, WorkingSetSize ') DO (
IF DEFINED $0 (
SET wss=%%i
CALL SET wss=0000000000000000000%%wss:~60%%
CALL SET wss=%%wss: =%%
CALL SET wss=$%%wss:~-20%%
CALL SET %%wss%%=%%i
) ELSE (SET $0=%%i)
)
FOR /f "tokens=1*delims==" %%i IN ('set $') DO ECHO(%%j
This should do your sort.
The sticking points are: the WMIC
command requires commas between fieldnames. These need to be escaped in the for/f
All environment variables starting "$" are first deleted, then each line of WMIC
is processed. The first line (the header) is saved in $0
then each line is saved in $size
The trick here is that SIZE in the listing is not only left-justified by space-padded, hence the string of zeroes is first prefixed to the contents of the workingsetsize column which probably actually begins in column 62 - but column 61 is a space. "60" is used since the substring facility counts from column 0, not column 1.
Then each space is replaced by [nothing], stripping out the trailing spaces (also conveniently, the space from col 61)
The last 20 characters from the resultant string form a leading-zero-filled version of the workingsetsize column.
prepend a $ to tha, and set the resultant variable to the contents of the line that generated it
Finally, listing the contents of the $
variables produces the required list in the required order.
Note that the claims that the WMIC
process output described is in order whatever is erroneous. Closer examination would reveal that it is not in order of ProcessID - either alphabetically (since that column is also left-justified) or alphabetically.
OK - revised version, should auto-adjust to width of process-name column:
@ECHO OFF
SETLOCAL
FOR /f "delims==" %%i IN ('set $ 2^>nul') DO SET "%%i="
SET /A COUNT=0
FOR /f "delims=" %%i IN ('wmic process get Name^, ProcessId^, WorkingSetSize ') DO (
IF DEFINED $0 (
SET wss=%%i
CALL %%lopcmd%%
CALL SET wss2=%%wss2: =%%
SET /A COUNT+=1
CALL SET wss=$%%wss2:~-20%%%%COUNT%%
CALL SET %%wss%%=%%i
) ELSE (SET $0=%%i&set/a wsscol=0&CALL :findcol&SET $0=%%i)
)
FOR /f "tokens=1*delims==" %%i IN ('set $') DO ECHO(%%j
GOTO :eof
:findcol
IF "%$0:~0,1%"=="W" SET lopcmd=CALL SET wss2=0000000000000000000%%wss:~%wsscol%%%&GOTO :eof
SET $0=%$0:~1%
SET /a wsscol+=1
GOTO findcol
No substantial changes - just calculate the width required by locating the "W" in the header and establishing an appropriate command to set the variable ready for processing...and which has to executed using the CALL %%var%%
method...
Minor edit: introducing COUNT to distinguish between lines with identical workingsetsizes. Count
is simply extends the variable-name used for sorting and makes the name unique.