Jump to content

Delete entries in the Textfile


Recommended Posts

Hi to all AUTOSCRIPT users. I am writing a script which saves log file in system directory. The log file will contains IP address of the PC. I wanted to delete some entries on the text file when it reaches a certain count. Like if it reached 20 entries, it will delete the first 10 entries and keep the remaining 10 entries. Is this possible in AUTOIT?

I have script like this:

Dim $aRecords

$CountLines = _FileCountLines(@SystemDir & "\pclog.txt")

If Not _FileReadToArray(@SystemDir & "\pclog.txt", $aRecords) Then

MsgBox(4096,"Error", " Error reading log to Array error:" & @error)

Exit

EndIf

If $CountLines > 20 Then

For $i = 1 to $aRecords[0]

;Msgbox(0,'Record:' & $i, $aRecords[$i])

;DELETE FIRST 10 ENTRY IN THE LOG FILE - i am stuck on this

Next

EndIf

Appreciate any of your help.

Thank you.

Link to comment
Share on other sites

Hi,

#include <file.au3>
Dim $aRecords

If Not _FileReadToArray(@SystemDir & "\pclog.txt", $aRecords) Then
    MsgBox(4096,"Error", " Error reading log to Array error:" & @error)
    Exit
EndIf

;If arraysize greater 20
If $aRecords [0] > 20 Then
    ;Open File in write mode, erasing previous contents 
    $file = FileOpen (@SystemDir & "\pclog.txt", 2)
    ;get rid of the 1 st 10 entries
    For $i = 11 to $aRecords[0]
        ;Msgbox(0,'Record:' & $i, $aRecords[$i])
        ;DELETE FIRST 10 ENTRY IN THE LOG FILE - i am stuck on this
        ;rewrite file with line 11 to .....
        FileWriteLine ($file, $aRecords [$i])
    Next
    FileClose ($file)
EndIf

;-))

Stefan

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...