Tom's Guide | Tom's Hardware | Tom's Games
![]() |
![]() |
![]() |
Hi, I wonder if this can be done using a Windows batch file - I need to delete all but the last 10 lines from a log file created by my antivirus application.
I have worked out how to take the entire contents of the virus scan log and append it to a log I have already started by using the following:
COPY "%FILENAME%"+"Virus_Removal\ClamWinPortable\Data\log\ClamWinLog.txt" "%FILENAME%" > NUL
However, the log that is generated lists every single file that has been scanned then adds a summary at the bottom. What I need to do is remove the huge list of files scanned (which can be any number of lines of text) and just keep/export the summary or just pull the summary from the log file and append it to another text file - either would be cool :)
I.E.
I need to keep the bold section:C:\Windo...... etc...
C:\WINDOWS\system32\IMM32.DLL: OK
C:\WINDOWS\system32\avgrsstx.dll: OK
C:\WINDOWS\system32\Apphelp.dll: OK
C:\WINDOWS\system32\MSGINA.dll: OK
C:\WINDOWS\system32\COMCTL32.dll: OK
C:\WINDOWS\system32\ODBC32.dll: OK
C:\WINDOWS\system32\comdlg32.dll: OK
Scanning aborted...----------- SCAN SUMMARY -----------
Known viruses: 472319
Engine version: 0.94.1
Scanned directories: 0
Scanned files: 39
Infected files: 0
Data scanned: 16.50 MBHope you guys 'n' gals can help with this one.

FOR /F "tokens=* delims=" %%A in ("log.txt") DO (
SET a=%%A
IF NOT !a:~0,1!==C ECHO %%A>>Keeper.txt
)I only Batch if possible, 2000 more lines of code, oh well.

Fantastic, thanks for your speedy repy. However, being a noob to batch scripting could you please advise how your code works?
And if poss how I can change the amount of log/lines at the end of the antivirus log to keep?
Thanks

That doesn't just keep your last 10 lines. I actually cheated a little :) It keeps any line that doesn't start with a C so with your example it will keep
Scanning aborted...
----------- SCAN SUMMARY -----------
Known viruses: 472319
Engine version: 0.94.1
Scanned directories: 0
Scanned files: 39
Infected files: 0
Data scanned: 16.50 MBIt is a FOR command... the first line sets the name of the text file to look through, as well as the variables name (%%A). The next two lines perform commands on it. First setting each line as a manipulatable variable (%a%) Then Parsing out all the lines that do not start with "C". It won't work if you use it on a drive that isn't C: Then you would need to change it a little.
I only Batch if possible, 2000 more lines of code, oh well.

if you have Windows services for Unix, you can use the tail command. eg
c:\> tail -10 file > newfile.txtor you can download from here.

@echo off > newfile& setLocal EnableDelayedExpansion
set /p K=this keeps the lasr N lines : how many do you need?
if !K!'==' goto :eoffor /f "tokens=* delims= " %%a in ('find /v /c "" ^< myfile') do (
set total=%%a
)set /a K=!total!-!K!
for /f "tokens=* delims= " %%a in (myfile) do (
set /a N+=1
if !N! gtr !K! echo %%a >> newfile
)
=====================================
If at first you don't succeed, you're about average.M2

![]() |
![]() |
![]() |

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.
| Ads by Google |