Search code examples
windowsbatch-filecmdtasklist

How to get I/O Reads from a specified task in the tasklist into a bat file variable


Using a Windows 7 .bat file, I would like to get the count of I/O Reads for a specific task into a variable.

I've tried tasklist.exe but it only shows memory usage and I'm not sure how to redirect that output into a batch file variable even if it showed I/O reads.

So, basically I would like to run in a command prompt a bat file which will place the I/O reads for a specific task (module name) into a %var% variable I can use later in the bat file.

Here is a way I can get the day, month, and year into a variable.

For /f "tokens=1-4 delims=/ " %%a in ("%DATE%") do (
    SET DAY=%%a
    SET YYYY=%%d
    SET MM=%%b
    SET DD=%%c
)

I would need a %TASKDATA% (instead of %DATE%) that contains the I/O reads so I can pull it out and assign it to another variable like this: SET IOREADS=%%d

Is there a way to do this using a windows command in a bat file?


Solution

  • I figure out the answer, posting in case it helps someone else later:

    @echo off
    wmic process where name="mstsc.exe" get readoperationcount /format:csv>tempuni.txt
    type tempuni.txt>temp.txt
    del tempuni.txt
    setLocal EnableDelayedExpansion
    for /f "skip=2 tokens=2 delims=, " %%a in (temp.txt) do (
        set /a N+=1
        set v%N%=%%a
    )
    del temp.txt
    set IOREADS=%v2%
    echo I/O Reads = %IOREADS%
    

    Note that wmic output is in unicode and I have to "type" the file to another file to convert it to ascii. Then I parse through the file contents to get the ioread count, in this case for the mstsc.exe process.