English 中文(简体)
Windows + wmic + memory
原标题:

I would like to sum used memory of all programs named "test.exe" in a batch script. I can use the following command to get memory usage of all the appropriate processes:

C:> wmic process where name="test.exe" get workingsetsize | findstr /v "WorkingSetSize"
55758848
66174976

So, I guess I would like to sum all those values together with a FOR loop. The following command would just display the i variable, but nevertheless I get the appended error.

C:> for /F %i IN ( wmic process where name="test.exe" get workingsetsize ^| findstr /v "WorkingSetSize" ) DO echo %i
 test.exe - Invalid alias verb.

How can I change that command to sum all the used memory together and output it and it must work in the CMD, preferably very similar to my command.

Thanks

最佳回答

Your FOR statement is failing because the = is a token delimiter and it is getting stripped by the batch parser. It will work if you escape it as ^=.

WMIC has an unfortunate "feature" that whenever its output is piped, it includes an extra carriage return character (0x0D) at the end of each line of output, plus an extra line at the end of the output. This can cause problems with extra unwanted lines in your FOR loop. Your situation is easily solved by changing the FINDSTR to look for at least one digit instead of the absence of the header line.

To get the sum you simply use SET /A in your loop (after first initializing the SUM variable).

>set SUM=0
>for /F %i IN ( wmic process where name^="test.exe" get workingsetsize ^| findstr "[0-9]" ) DO set /a SUM+=%i

However - math in Windows CMD is limited to 32bit signed integer support. Your sum could easily exceed the maximum allowed value of 2,147,483,647. You would be better off using PowerShell, or else CSCRIPT to execute a VBS or JScript program. All of those alternatives support long integers (64 bit).

问题回答

暂无回答




相关问题
Why running a service as Local System is bad on windows?

I am trying to find out the difference between difference service account types. I tumbled upon this question. The answer was because it has powerful access to local resources, and Network Service ...

Programmatically detect Windows cluster configuration?

Does anyone know how to programatically detect that a Windows server is part of a cluster? Further, is it possible to detect that the server is the active or passive node? [Edit] And detect it from ...

get file icon for Outlook appointment (.msg)

I ve read Get File Icon used by Shell and the other similar posts - and already use SHFileInfo to get the associated icon for any given extension, and that works great. However, Outlook uses ".msg" ...

Identifying idle state on a windows machine

I know about the GetLastInputInfo method but that would only give me the duration since last user input - keyboard or mouse. If a user input was last received 10 minutes ago, that wouldn t mean the ...

Terminating a thread gracefully not using TerminateThread()

My application creates a thread and that runs in the background all the time. I can only terminate the thread manually, not from within the thread callback function. At the moment I am using ...

热门标签