Jump to content

Help delete lines from text


magace
 Share

Recommended Posts

Ok so i use this code to delete $line from a text file

 

Global $success = False
$file_name = "C:\Users\awpclol\Desktop\mon\files\pkaccount.txt"
$line_text_input = $line
$file_count_lines = _FileCountLines($file_name)
for $i = 0 to $file_count_lines
    $Lines_text_output = FileReadLine($file_name, $i)
    if StringInStr($Lines_text_output, $line_text_input) then
        _FileWriteToLine($file_name, $i, "", 1)
        $success = True
        ExitLoop
    EndIf
Next
if $success = True Then
   ToolTip("deleting line",0,0)
Else
    ToolTip("user not found",0,0)
EndIf

How do I make it go in a loop so it deletes all the intsances.  Now it only deletes the first $line in the list not every entry.

someone said delete ExitLoop

it still seems to only delete a few not all entrys.

Edited by magace
Link to comment
Share on other sites

  • Moderators

Rather than the old _FileCountLine method, I would suggest just reading the whole file into an array and then parsing through it. You can then write the results to an output file. Something like this for example:

#include <Array.au3>
#include <File.au3>

Local $aArray, $aTemp[1], $file, $text, $output
$file = @DesktopDir & "\pkaccount.txt"
$output = @DesktopDir & "\output.txt"
$text = "Bandersnatch"

_FileReadToArray($file, $aArray)

    For $i = 1 To $aArray[0]
        If StringInStr($aArray[$i], $text) Then _ArrayAdd($aTemp, $aArray[$i])
    Next

_FileWriteFromArray($output, $aTemp)

"Profanity is the last vestige of the feeble mind. For the man who cannot express himself forcibly through intellect must do so through shock and awe" - Spencer W. Kimball

How to get your question answered on this forum!

Link to comment
Share on other sites

If the file is too large to read into memory, you can use this method as well.

It also doesn't use _FileCountLine

Global $success = False
$file_name = "C:\Users\awpclol\Desktop\mon\files\pkaccount.txt"
$hFile = FileOpen($file_name, 0)
$line_text_input = $line
$i = 1
While 1
    $Lines_text_output = FileReadLine($hFile)
    If @error = -1 then ExitLoop
    If StringInStr($Lines_text_output, $line_text_input) Then
        _FileWriteToLine($file_name, $i, "", 1)
        $success = True
        ExitLoop
    EndIf
    $i += 1
WEnd
FileClose($hFile)
If $success = True Then
    ToolTip("deleting line", 0, 0)
Else
    ToolTip("user not found", 0, 0)
EndIf

If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.
Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag Gude
How to ask questions the smart way!

I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from.

Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.  -  ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.  -  Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.  -  _FileGetProperty - Retrieve the properties of a file  -  SciTE Toolbar - A toolbar demo for use with the SciTE editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator

Link to comment
Share on other sites

 

If the file is too large to read into memory, you can use this method as well.

It also doesn't use _FileCountLine

Global $success = False
$file_name = "C:\Users\awpclol\Desktop\mon\files\pkaccount.txt"
$hFile = FileOpen($file_name, 0)
$line_text_input = $line
$i = 1
While 1
    $Lines_text_output = FileReadLine($hFile)
    If @error = -1 then ExitLoop
    If StringInStr($Lines_text_output, $line_text_input) Then
        _FileWriteToLine($file_name, $i, "", 1)
        $success = True
        ExitLoop
    EndIf
    $i += 1
WEnd
FileClose($hFile)
If $success = True Then
    ToolTip("deleting line", 0, 0)
Else
    ToolTip("user not found", 0, 0)
EndIf

This still wont delete multiple lines. As is it deletes one line even if multiple.  If I comment out the exitloop it deleted 3/6.

edit

tested with 10 of same entry deletes half always

Edited by magace
Link to comment
Share on other sites

This still wont delete multiple lines. As is it deletes one line even if multiple.  If I comment out the exitloop it deleted 3/6.

Delete the ExitLoop inside the If statement.

Edited by BrewManNH

If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.
Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag Gude
How to ask questions the smart way!

I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from.

Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.  -  ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.  -  Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.  -  _FileGetProperty - Retrieve the properties of a file  -  SciTE Toolbar - A toolbar demo for use with the SciTE editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator

Link to comment
Share on other sites

Oops, deleted my post, but it's correct:

you need to reverse through the file backwards:

Global $success = False
$file_name = "C:\Users\awpclol\Desktop\mon\files\pkaccount.txt"
$hFile = FileOpen($file_name, 0)
$line_text_input = $line
$i = _FileCountLines($file_name)
While 1
    $Lines_text_output = FileReadLine($hFile,$i)
    If @error = -1 then ExitLoop
    If StringInStr($Lines_text_output, $line_text_input) Then
        _FileWriteToLine($file_name, $i, "", 1)
        $success = True
        ExitLoop
    EndIf
    $i -= 1
WEnd
FileClose($hFile)
If $success = True Then
    ToolTip("deleting line", 0, 0)
Else
    ToolTip("user not found", 0, 0)
EndIf
Edited by jdelaney
IEbyXPATH-Grab IE DOM objects by XPATH IEscriptRecord-Makings of an IE script recorder ExcelFromXML-Create Excel docs without excel installed GetAllWindowControls-Output all control data on a given window.
Link to comment
Share on other sites

Ooops my bad, I didn't realize that _FileWriteToLine would delete the whole line if you used "".

Try this version, it uses 2 files, one to read from, the other to write only the correct lines to.

Global $success = False
$file_name = "C:\Users\awpclol\Desktop\mon\files\pkaccount.txt"
$file_name1 = "C:\Users\awpclol\Desktop\mon\files\pkaccount1.txt"
$hFile = FileOpen($file_name, 0)
$hOutFile = FileOpen($file_name1, 2)
$line_text_input = $line
$i = 1
While 1
    $Lines_text_output = FileReadLine($hFile)
    If @error = -1 then ExitLoop
    If Not StringInStr($Lines_text_output, $line_text_input) Then
        FileWriteLine($hOutFile, $Lines_text_output)
    Else
        $success = True
    EndIf
WEnd
FileClose($hFile)
FileClose($hOutFile)
#cs
; if you want to keep the same file name, then uncomment this section.
    FileCopy($file_name1, $file_name)
#ce

If $success = True Then
    ToolTip("deleting line", 0, 0)
Else
    ToolTip("user not found", 0, 0)
EndIf

It's probably a lot faster too.

If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.
Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag Gude
How to ask questions the smart way!

I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from.

Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.  -  ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.  -  Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.  -  _FileGetProperty - Retrieve the properties of a file  -  SciTE Toolbar - A toolbar demo for use with the SciTE editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator

Link to comment
Share on other sites

Ooops my bad, I didn't realize that _FileWriteToLine would delete the whole line if you used "".

Try this version, it uses 2 files, one to read from, the other to write only the correct lines to.

Global $success = False
$file_name = "C:\Users\awpclol\Desktop\mon\files\pkaccount.txt"
$file_name1 = "C:\Users\awpclol\Desktop\mon\files\pkaccount1.txt"
$hFile = FileOpen($file_name, 0)
$hOutFile = FileOpen($file_name1, 2)
$line_text_input = $line
$i = 1
While 1
    $Lines_text_output = FileReadLine($hFile)
    If @error = -1 then ExitLoop
    If Not StringInStr($Lines_text_output, $line_text_input) Then
        FileWriteLine($hOutFile, $Lines_text_output)
    Else
        $success = True
    EndIf
WEnd
FileClose($hFile)
FileClose($hOutFile)
#cs
; if you want to keep the same file name, then uncomment this section.
    FileCopy($file_name1, $file_name)
#ce

If $success = True Then
    ToolTip("deleting line", 0, 0)
Else
    ToolTip("user not found", 0, 0)
EndIf

It's probably a lot faster too.

thanks this worked I did this and it saves to same file instead of copying the file is that wrong?

Global $success = False
$file_name = "C:\Users\awpclol\Desktop\mon\files\pkaccount.txt"
$file_name1 = "C:\Users\awpclol\Desktop\mon\files\pkaccount.txt"
$hFile = FileOpen($file_name, 0)
$hOutFile = FileOpen($file_name1, 2)
$line_text_input = $line
$i = 1
While 1
    $Lines_text_output = FileReadLine($hFile)
    If @error = -1 then ExitLoop
    If Not StringInStr($Lines_text_output, $line_text_input) Then
        FileWriteLine($hOutFile, $Lines_text_output)
    Else
        $success = True
    EndIf
WEnd
FileClose($hFile)
FileClose($hOutFile)
Edited by magace
Link to comment
Share on other sites

no, you would have to delete the original (on completion of the loop), and replace it with the new (through function: filemove), or you will have your same, original problem.

I'd create a backup, prior to doing so.

The delete isn't necessary if you include the proper flag on filemove

...hmm, does the whole file get read into memory twice, or are they sharing the same data...maybe that would work with 2 read ins...

Edited by jdelaney
IEbyXPATH-Grab IE DOM objects by XPATH IEscriptRecord-Makings of an IE script recorder ExcelFromXML-Create Excel docs without excel installed GetAllWindowControls-Output all control data on a given window.
Link to comment
Share on other sites

I did and it deletes half of the instances

I put 

dom

dom

dom

dom

in the file then ran it and it deletes 2 doms

 

I assume from the code posted so far that the word freedom should be deleted, due to the use of StringInStr(). This is not clear in your example. Here's another method.

;

Local $hFile = FileOpen(@ScriptDir & "\test.txt") ; open for reading
If $hFile = -1 Then Exit ; Unable to open file

Local $sString = FileRead($hFile)
If @error Then
    FileClose($hFile) ; Unable to read file
    Exit
EndIf
FileClose($hFile)

Local $sRemove = "dom"
Local $sNewString = StringRegExpReplace($sString, "(\V*" & $sRemove & "\V*)(\v{1,2}|\z)", "")

$hFile = FileOpen(@ScriptDir & "\result.txt", BitOr(8,1)) ; open for writing
If $hFile = -1 Then Exit ; Unable to open file

FileWrite($hFile, $sNewString)
FileClose($hFile)
Link to comment
Share on other sites

Will that delete the line the word is found on, or just the word itself?

If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.
Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag Gude
How to ask questions the smart way!

I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from.

Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.  -  ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.  -  Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.  -  _FileGetProperty - Retrieve the properties of a file  -  SciTE Toolbar - A toolbar demo for use with the SciTE editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator

Link to comment
Share on other sites

You'll have to do better than that, as I have no idea what you mean.

If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.
Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag Gude
How to ask questions the smart way!

I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from.

Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.  -  ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.  -  Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.  -  _FileGetProperty - Retrieve the properties of a file  -  SciTE Toolbar - A toolbar demo for use with the SciTE editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator

Link to comment
Share on other sites

Simply, does it delete just the word, or does it delete the line the word is on, from the start of the line to the end of the line regardless of where in that line the word is found? I could run the code to test it, but I'm on a computer with no AutoIt so I can't at the moment do that.

If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.
Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag Gude
How to ask questions the smart way!

I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from.

Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.  -  ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.  -  Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.  -  _FileGetProperty - Retrieve the properties of a file  -  SciTE Toolbar - A toolbar demo for use with the SciTE editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator

Link to comment
Share on other sites

I could run the code to test it, but I'm on a computer with no AutoIt so I can't at the moment do that.

 

I'm sorry, I thought you were being lazy. :ph34r:  It deletes all the lines which contain the expression "dom". There is still room for improvement because it may also delete an empty line after a match, if a single line break character is used to terminate each line. I need to have a think about that. Perhaps it's a bit too greedy.


Edit: Try replacing the regexp in post 12 with this one.

Local $sNewString = StringRegExpReplace($sString, "(\V*" & $sRemove & "\V*)(\r\n|[\r\n\z])", "")

;

This leaves all empty lines intact regardless of the line termination characters used.

Edited by czardas
Link to comment
Share on other sites

Here I would like to add that I was reluctant to give an answer earlier because some users will learn more by working through the regular expressions themselves. Obviously it is harder to figure it out if you are unable to run the code.

"(V*" & $sRemove & "V*)(rn|[rnz])"

Implies the following sequence.

( ...Start of ordered group

V ...anything which is not a line break (or other vertical space character)

* ...may or may not appear any number of times followed by

& $sRemove & ...the search term (dom) followed by

V ...anything which is not a line break

* ...may or may not appear any number of times followed by

) ...End of group

( ...Start of ordered group

rn ...@CRLF

| ...OR

[rnz] ... any one of the characters @CR OR @LF OR the end of the string (not repeated)

) ...End of group

Edited by czardas
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...