CPU Time Column

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
booga73
Posts: 108
Joined: 30 Nov 2011 16:16

CPU Time Column

#1 Post by booga73 » 23 Oct 2012 13:43

I am trying to extract a column of information from the command, tasklist /V.

I can get the following columns: Image Name, PID, Session Name, Session #, and Mem Usage.

The column which I need to collect is CPU Time.

The User Name column has delimiters that I just don't know how to figure out to skip completely and get the CPU Time column. I plucked away the code below but haven't been able to get the column for CPU Time. Please advise what I should do or if a more simple approach can help.


tasklist /V > c:\taskListV.txt
more +5 c:\taskListV.txt > c:\NewList.txt

for /F "eol=; tokens=1,2,3,4,5,6 delims=* " %%i in (c:\NewList.txt) do echo %%i %%j %%k %%l %%m %%n >> c:\CPUtime.txt

Boombox
Posts: 80
Joined: 18 Oct 2012 05:51

Re: CPU Time Column

#2 Post by Boombox » 23 Oct 2012 15:10

.

Code: Select all

TASKLIST /V /FO LIST

.

That should help...

booga73
Posts: 108
Joined: 30 Nov 2011 16:16

Re: CPU Time Column

#3 Post by booga73 » 23 Oct 2012 15:24

Hi Boombox,

Thank you for your follow up. Your reply provides an alternative way to list processes and, session name, mem use, etc ..

I need though, the CPU Time column output to text file, not another list.

I would like to keep the cpu time column intact, so that I can pair up image name to PID, to Session Name, mem usage an then CPU time, all into columns.

For instance the command, typing the command: tasklist /V > c:\taskListV.txt
at the dos prompt to pipe to text file will show 9 different columns, but I would like to extract the cpu time column.

Still needing assistance. . .

Squashman
Expert
Posts: 4488
Joined: 23 Dec 2011 13:59

Re: CPU Time Column

#4 Post by Squashman » 23 Oct 2012 15:37

What do you mean the username has delimiters?
You should be able to use the csv output and use a comma as the delimiter.

booga73
Posts: 108
Joined: 30 Nov 2011 16:16

Re: CPU Time Column

#5 Post by booga73 » 23 Oct 2012 15:56

for instance, at the command prompt.. .
type the following:

tasklist /V > c:\taskListV.txt

That command, once you open up tasklistV.txt, that will show you all the columns generated from tasklist /v command. The seventh column in that text is User Name.


When you open up c:\taskListV.txt, the user name column shows the following ownership of processes: NT AUTHORITY\SYSTEM, N/A, NT AUTHORITY\NETWORK SERVICE

I would like to know where in my code that I can get tokens 1-6, which already output to text file accordingly to what I need, but then skip the column that lists User Name and proceed with getting column that shows CPU Time.

Boombox
Posts: 80
Joined: 18 Oct 2012 05:51

Re: CPU Time Column

#6 Post by Boombox » 23 Oct 2012 17:10

Okay. So this code gets everything to file.

Code: Select all

FOR /F "TOKENS=1-20 DELIMS=," %%A IN ('TASKLIST /V /FO CSV') DO (
ECHO %%A %%B %%C %%D %%E %%F %%G %%H %%I %%J %%K %%L %%M %%N %%O %%P %%Q %%R %%S %%T>>OUTPUT.TXT)


And you can use some variation of this code later, to remove the speech marks.

Code: Select all

FOR /F TOKENS^=1-20^ DELIMS^=^" %%B IN (output.txt)  DO (...........

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: CPU Time Column

#7 Post by foxidrive » 23 Oct 2012 17:40

This works here:

Code: Select all

@echo off
for /f tokens^=1^,15^ delims^=^" %%a in ('tasklist /v /fo csv') do echo %%b %%a
pause

Squashman
Expert
Posts: 4488
Joined: 23 Dec 2011 13:59

Re: CPU Time Column

#8 Post by Squashman » 23 Oct 2012 18:02

foxidrive wrote:This works here:

Code: Select all

@echo off
for /f tokens^=1^,15^ delims^=^" %%a in ('tasklist /v /fo csv') do echo %%b %%a
pause

Quote delimiter strikes again.

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: CPU Time Column

#9 Post by foxidrive » 23 Oct 2012 18:14

Squashman wrote:Quote delimiter strikes again.


It's the first time I've put it into practice, and very useful too.

You can't count commas because in a RAM field you could get two commas in 1,000,000 or one comma in 500,000 and so the comma positions change.

booga73
Posts: 108
Joined: 30 Nov 2011 16:16

Re: CPU Time Column

#10 Post by booga73 » 24 Oct 2012 13:49

Thank you Boombox, Foxidrive, and Squashman for your input & support! :P

Boombox, I like the final output solution; you broke down the columns in the text output, nicely.

Squashman, your solution, the output shows simplified, no need for text output, which is good too.


both of you used CSV file output, correct me if I'm not right, /FO CSV parameter.

my request for support is now resolved,

Thank you both and thank you for dostips.com! v/r Booga73

Ed Dyreen
Expert
Posts: 1569
Joined: 16 May 2011 08:21
Location: Flanders(Belgium)
Contact:

Re: CPU Time Column

#11 Post by Ed Dyreen » 25 Oct 2012 01:17

'
If you like to collect all columns data including column 9: 'winTitle', neither quote- nor comma-delimiter work reliably.

Code: Select all

title ,","
A general solution that reads all/any column data correctly.

Code: Select all

>taskList /nh /v /fo csv

"System Idle Process","0","Console","0","28 kB","Actief","NT AUTHORITY\SYSTEM","12:03:38","n.v.t."
...
"cmd.exe","364","Console","0","2.580 kB","Actief","ED-SERV-0\Administrator","0:00:00",""," - taskList /nh /v /fo csv"
The result is CSV delimited, this can be read by batch without having to worry about inline quotes nor commas ( except for column 9 which is read at the end ).

Code: Select all

for /f "delims=" %%? in (
       'taskList /nh /v /fo csv'
) do (
       set c=&set 0=&for %%? in (%%?) do if !c! lss 8 set/ac+=1&set 0=!0!,%%?&set !c!=%%~?
       set 0=!0:~1!
       set ?=%%?&for /f ^"eol^=^%$lf%%$lf%delims^=^" %%r in ("!0!") do set 9=!?:%%r,=!
       set 0=!0!,!9!
)
    1) read CSV delimited entireLine.
    2) Split the tokens using the default delimiters and drop the outer quotes.
    3) column[9] = entireLine.stringTrimLeft(columns[1-8])

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: CPU Time Column

#12 Post by foxidrive » 25 Oct 2012 01:51

Ed Dyreen wrote:If you like to collect all columns data including column 9: 'winTitle', neither quote- nor comma-delimiter work reliably.


I can't see how this will fail, when using quote as a delimiter.

Code: Select all

@echo off
for /f tokens^=1-17^ delims^=^" %%a in ('tasklist /nh /v /fo csv') do (
echo %%a %%c %%e %%g %%i %%k %%m %%o %%q
)


If there are quotes in the last token then this will remove them, but I didn't see any quotes in the description.


Code: Select all

@echo off
for /f tokens^=1-16*^ delims^=^" %%a in ('tasklist /nh /v /fo csv') do (
set "end=%%q"
setlocal enabledelayedexpansion
set "end=!end:~0,-1!"
echo %%a %%c %%e %%g %%i %%k %%m %%o !end!
endlocal
)

Ed Dyreen
Expert
Posts: 1569
Joined: 16 May 2011 08:21
Location: Flanders(Belgium)
Contact:

Re: CPU Time Column

#13 Post by Ed Dyreen » 25 Oct 2012 02:58

foxidrive wrote:I can't see how this will fail, when using quote as a delimiter.

If there are quotes in the last token then this will remove them, but I didn't see any quotes in the description.
Exactly :wink:

Code: Select all

@echo off &title ,"""""," my,"," Title
set $lf=^


::
setlocal enabledelayedexpansion

for /f "delims=" %%? in (
       'taskList /nh /v /fo csv'
) do (
       set c=&set 0=&for %%? in (%%?) do if !c! lss 8 set/ac+=1&set 0=!0!,%%?&set !c!=%%~?
       set 0=!0:~1!
       set ?=%%?&for /f ^"eol^=^%$lf%%$lf%delims^=^" %%r in ("!0!") do set 9=!?:%%r,=!
       set 0=!0!,!9!
       set 9=!9:~1,-1!
       echo.
       echo.?=%%?_
       for /l %%? in (0,1,9) do echo.%%?=!%%?!_
)

pause

for /f "delims=" %%? in (
       'taskList /nh /v /fo csv'
) do (
       echo.
       echo.?=%%?_
)
pause

for /f tokens^=1-16*^ delims^=^" %%a in (
       'taskList /nh /v /fo csv'
) do (
       set "1=%%a"
       set "2=%%c"
       set "3=%%e"
       set "4=%%g"
       set "5=%%i"
       set "6=%%k"
       set "7=%%m"
       set "8=%%o"
       set "9=%%q"
       set "9=!9:~0,-1!"

       set "0="%%a","%%c","%%e","%%g","%%i","%%k","%%m","%%o","%%q""
       set "0=!0:~0,-1!"
       echo.
       for /l %%? in (0,1,9) do echo.%%?=!%%?!_
)

pause
exit

Code: Select all

?="cmd.exe","2968","Console","0","1.496 kB","Actief","ED-SERV-0\Administrator","
0:00:00",""""""," my,"," Title"_
0="cmd.exe","2968","Console","0","1.496 kB","Actief","ED-SERV-0\Administrator","
0:00:00",""""""," my,"," Title"_
1=cmd.exe_
2=2968_
3=Console_
4=0_
5=1.496 kB_
6=Actief_
7=ED-SERV-0\Administrator_
8=0:00:00_
9="""""," my,"," Title_

Code: Select all

?="cmd.exe","2968","Console","0","2.128 kB","Actief","ED-SERV-0\Administrator","
0:00:00",""""""," my,"," Title"_

Code: Select all

0="cmd.exe","2968","Console","0","2.148 kB","Actief","ED-SERV-0\Administrator","
0:00:00","," my,"," Title"_
1=cmd.exe_
2=2968_
3=Console_
4=0_
5=2.148 kB_
6=Actief_
7=ED-SERV-0\Administrator_
8=0:00:00_
9=," my,"," Title_

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: CPU Time Column

#14 Post by foxidrive » 25 Oct 2012 03:21

Ed Dyreen wrote:
foxidrive wrote:I can't see how this will fail, when using quote as a delimiter.

If there are quotes in the last token then this will remove them, but I didn't see any quotes in the description.
Exactly :wink:


So it won't fail as the descriptions don't contain quotes.

Post Reply