Jump to content

AutoIt delete all lines from a *.txt file except some


Recommended Posts

In case you are not aware.  When using SciTE as your AutoIt script text editor, and you run a script within SciTE, "ConsoleWrite()" will write to the output pane. The output pane is the lower window of the SciTE editor, and on occasions, shows the script's errors which helps in debugging.

Code has been added to have /: put in the clipboard only while the Adlib function, _FileDeleteLines(), is running.

HotKeySet("{ESC}", "Terminate") ; Press Esc key to exit script.

Global $sFileName = "281478146260429_chatlog.txt"
Global $sSearch4 = "tips you"
Global $sSearch5 = "bank credits"
Global $sSearch6 = "trade with you"
Global $sSearch7 = "you tell"
Global $sSearch8 = "to a duel"
_FileDeleteLines()
AdlibRegister("_FileDeleteLines", 10 * 1000) ; Run the function, _FileDeleteLines(), every 10 secs

While Sleep(100)
WEnd


Func _FileDeleteLines()
    local  $sCBOld = ClipGet() ; Store clipboard contents for restoration later.
    ClipPut("/:")
    Local $sFileContents = FileRead($sFileName)
    Local $hFileOpen = FileOpen($sFileName, 2) ; $FO_OVERWRITE (2) = Write mode (erase previous contents)

    FileWrite($hFileOpen, StringRegExpReplace($sFileContents, _
            '(?m)^.*(\Q' & $sSearch4 & "\E|\Q" & $sSearch5 & "\E|\Q" & $sSearch6 & "\E|\Q" & $sSearch7 & "\E|\Q" & $sSearch8 & _
            '\E.*\R?)(*SKIP)(?!)|^.*\R*', ""))
    FileClose($hFileOpen)

    ConsoleWrite(ClipGet() & @CRLF) ; Show contents of clipboard
    ClipPut($sCBOld) ; Restore contents of clipboard
    ConsoleWrite(ClipGet() & @CRLF) ; Show contents of clipboard
EndFunc   ;==>_FileDeleteLines

Func Terminate()
    AdlibUnRegister("_FileDeleteLines")
    Exit
EndFunc   ;==>Terminate

 

Link to comment
Share on other sites

 

On 12/5/2017 at 0:31 AM, Malkey said:

Alright this is all working great. But after a while, if there's no activity, the script will terminate. Is there a way to make sure it's always running? The script is saved as SCRIPT.exe

HotKeySet("{ESC}", "Terminate") ; Press Esc key to exit script.

Global $sFileName = "281478146260429_chatlog.txt"
Global $sSearch4 = "tips you"
Global $sSearch5 = "bank credits"
Global $sSearch6 = "trade with you"
Global $sSearch7 = "you tell"
Global $sSearch8 = "to a duel"
_FileDeleteLines()
AdlibRegister("_FileDeleteLines", 10 * 1000) ; Run the function, _FileDeleteLines(), every 10 secs

While Sleep(100)
WEnd


Func _FileDeleteLines()
    local  $sCBOld = ClipGet() ; Store clipboard contents for restoration later.
    ClipPut("/:")
    Local $sFileContents = FileRead($sFileName)
    Local $hFileOpen = FileOpen($sFileName, 2) ; $FO_OVERWRITE (2) = Write mode (erase previous contents)

    FileWrite($hFileOpen, StringRegExpReplace($sFileContents, _
            '(?m)^.*(\Q' & $sSearch4 & "\E|\Q" & $sSearch5 & "\E|\Q" & $sSearch6 & "\E|\Q" & $sSearch7 & "\E|\Q" & $sSearch8 & _
            '\E.*\R?)(*SKIP)(?!)|^.*\R*', ""))
    FileClose($hFileOpen)

    ConsoleWrite(ClipGet() & @CRLF) ; Show contents of clipboard
    ClipPut($sCBOld) ; Restore contents of clipboard
    ConsoleWrite(ClipGet() & @CRLF) ; Show contents of clipboard
EndFunc   ;==>_FileDeleteLines

Func Terminate()
    AdlibUnRegister("_FileDeleteLines")
    Exit
EndFunc   ;==>Terminate

 

 

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...