CPU Time Column
Moderator: DosItHelp
CPU Time Column
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
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
Re: CPU Time Column
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. . .
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. . .
Re: CPU Time Column
What do you mean the username has delimiters?
You should be able to use the csv output and use a comma as the delimiter.
You should be able to use the csv output and use a comma as the delimiter.
Re: CPU Time Column
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.
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.
Re: CPU Time Column
Okay. So this code gets everything to file.
And you can use some variation of this code later, to remove the speech marks.
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 (...........
Re: CPU Time Column
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
Re: CPU Time Column
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.
Re: CPU Time Column
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.
Re: CPU Time Column
Thank you Boombox, Foxidrive, and Squashman for your input & support!
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

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
Re: CPU Time Column
'
If you like to collect all columns data including column 9: 'winTitle', neither quote- nor comma-delimiter work reliably.
A general solution that reads all/any column data correctly.
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 ).
If you like to collect all columns data including column 9: 'winTitle', neither quote- nor comma-delimiter work reliably.
Code: Select all
title ,","
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"
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])
Re: CPU Time Column
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
)
Re: CPU Time Column
Exactlyfoxidrive 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.

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_
Re: CPU Time Column
Ed Dyreen wrote:Exactlyfoxidrive 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.![]()
So it won't fail as the descriptions don't contain quotes.