samiam41
asked on
Add content to output file
Hey Experts! I have a script that I use to list the processes running and output the filtered results to a log file. During the creation of this script file (done by an EE expert), I somehow lost the ability to echo the date and time of when the script was ran. Since this script will serve as a sort of "process snap-shot" and ran at varying times, I need the time stamp.
Your assistance in adding this functionality to the existing script would be much appreciated.
Your assistance in adding this functionality to the existing script would be much appreciated.
@echo off
setlocal enabledelayedexpansion
set Log=c:\tools\logs\audit.log
set Columns=1 7 8
cd\
cd tools\pstools
echo. >> %log%
echo. >> %log%
echo %date% %time: =0% >> %log%
(set EmptyString= )
set /a i = 0
for %%a in ("Name,20,Left" "Pid,5,Right" "Pri,4,Right" "Thd,4,Right" "Hnd,5,Right" "Priv,7,Right" "CPU Time,16,Right" "Elapsed Time,16,Right") do (
set /a i += 1
for /f "tokens=1-3 delims=," %%b in (%%a) do (
call :Adjust%%d Col[!i!].Name %%c %%b
set Col[!i!].Width=%%c
set Col[!i!].Adjust=%%d
)
)
set Header=
for %%a in (%Columns%) do set Header=!Header!!Col[%%a].Name!
echo %Header%
>"%Log%" echo %Header%
for /f "skip=4 tokens=1-8" %%a in ('pslist.exe ^| findstr.exe /v /i /r /c:"^chrome\>" /c:"^sVchost\>"') do (
call :Adjust!Col[1].Adjust! Col1 !Col[1].Width! %%a
call :Adjust!Col[2].Adjust! Col2 !Col[2].Width! %%b
call :Adjust!Col[3].Adjust! Col3 !Col[3].Width! %%c
call :Adjust!Col[4].Adjust! Col4 !Col[4].Width! %%d
call :Adjust!Col[5].Adjust! Col5 !Col[5].Width! %%e
call :Adjust!Col[6].Adjust! Col6 !Col[6].Width! %%f
call :Adjust!Col[7].Adjust! Col7 !Col[7].Width! %%g
call :Adjust!Col[8].Adjust! Col8 !Col[8].Width! %%h
set Row=
for %%z in (%Columns%) do set Row=!Row!!Col%%z!
echo !Row!
>>"%Log%" echo !Row!
)
goto :eof
:AdjustLeft <variable> <length> <string>
set __tmp=%~3%EmptyString%
set %~1=!__tmp:~0,%~2!
goto :eof
:AdjustRight <variable> <length> <string>
set __tmp=%EmptyString%%~3
set %~1=!__tmp:~-%~2!
goto :eof
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
I love it! Thanks Qlemo. Good to see you're still here making this site the best on the web,
ASKER