Jump to content

Delete double lines from txt file


Recommended Posts

Why doesn't this work?

#include <file.au3>

$file = FileOpen("test.txt", 0)
$save = FileOpen("result.txt", 2)
; Check if file opened for reading OK
If $file = -1 Then
    MsgBox(0, "Error", "Unable to open file.")
    Exit
EndIf

$CountLines = _FileCountLines("test.txt")
MsgBox(64, "Error log recordcount", "There are " & $CountLines & " in the test.txt.")


For $i = 1 to $CountLines Step +1
$line1 = FileReadLine($file,$i)
If @error = -1 Then ExitLoop
MsgBox(0, "$Line1:", $line1)
While 1
    $line2 = FileReadLine($file)
    If @error = -1 Then ExitLoop
    MsgBox(0, "$Line2:", $line2)
    If $line1 = $line2 then call ("_Cleanup")
;if not ($line1 = $line2) then call ("_Add") <--------------- why doesn.t it work?
Wend
Next

Func _Cleanup()
    
    MsgBox(0, "Cleanup:", $line2)
    $save = FileOpen("result.txt", 1)

; Check if file opened for writing OK
If $save = -1 Then
    MsgBox(0, "Error", "Unable to open file.")
    Exit
EndIf

FileWriteLine($save, $line2 & @CRLF)


FileClose($save)

EndFunc

Func _Add()
    MsgBox(0, "$ADD:", $line1)
    $save = FileOpen("result.txt", 1)


If $save = -1 Then
    MsgBox(0, "Error", "Unable to open file.")
    Exit
EndIf

FileWriteLine($save, $line2 & @CRLF)


FileClose($save)
EndFunc
; Read in lines of text until the EOF is reached


FileClose($file)
Edited by gempie
Link to comment
Share on other sites

Why doesn't this work?

If $line1 = $line2 then call ("_Cleanup")
;if not ($line1 = $line2) then call ("_Add") <--------------- why doesn.t it work?
This is an easier method as only testing the condition once.

If $line1 = $line2 then
    _Cleanup()
Else
    _Add()
EndIf

:D

Link to comment
Share on other sites

I FINALY DID IT!

HERE's THE CODE TO DELETE DOUBLE LINES FROM A FILE:

#include <Date.au3>
 #include <file.au3>
dim $line1, $line2

$file = FileOpen("patient.txt", 0)

; Check if file opened for reading OK
If $file = -1 Then
    MsgBox(0, "Error", "Unable to open file.")
    Exit
EndIf

$CountLines = _FileCountLines("patient.txt")


For $i = 1 to $CountLines Step +1
$line1 = FileReadLine($file,$i)
If @error = -1 Then ExitLoop

While 1
    $line2 = FileReadLine($file)
    If @error = -1 Then ExitLoop
    If $line1 = $line2 then call ("_Cleanup")

Wend
Next

Func _Cleanup()
    
    $save = FileOpen("result.txt", 1)

; Check if file opened for writing OK
If $save = -1 Then
    MsgBox(0, "Error", "Unable to open file.")
    Exit
EndIf

FileWriteLine($save, $line2 & @CRLF)

FileClose($save)


EndFunc
$save2 = FileOpen("result.txt", 1)
FileWriteLine($save2, "End" & @CRLF);<-------- here's an error change End to ENDING to fix
FileClose($save2)
FileClose($file)
$filex = FileOpen("patient.txt", 1)
FileWriteLine($filex, "End" & @CRLF);<-------- here's an error change End to ENDING to fix
FileClose($filex)
;-----------
$file = FileOpen("result.txt", 0)
$filex = FileOpen("patient.txt", 0)
If $file = -1 Then
    
    Exit
EndIf

If $filex = -1 Then
    
    Exit
EndIf


$save = ""
; Read in lines of text until the EOF is reached
$CountLines = _FileCountLines("result.txt")


For $i = 1 to $CountLines Step +1
$line1 = FileReadLine($file,$i)
If @error = -1 Then ExitLoop
while 1
    $line = FileReadLine($filex)
    If @error = -1 Then ExitLoop
    If Not StringInStr($line, $line1)Then $save = $save & $line & @CRLF
    if $line = $line1 then ExitLoop
WEnd
Next




FileClose($file)
Fileclose($filex)
$end = FileOpen("patient.txt", 2)
FileWrite($end, $save)
Fileclose($end)
FileDelete("result.txt")

EDIT: THIS CODE HAS ERRORS IN IT! (SEE <---------)

Edited by gempie
Link to comment
Share on other sites

I'll Bet NOBODY has an answer to this one. There was an error in my script. The only thing i changed is End (which became a var) to ENDING. (see<-----------) This error occasionally left out a line in the patient.txt

New CODE: (COST me 2 hours to figure this one out)

#include <Date.au3>
#include <file.au3>
dim $line1, $line2

$file = FileOpen("patient.txt", 0)

; Check if file opened for reading OK
If $file = -1 Then
    MsgBox(0, "Error", "Unable to open file.")
    Exit
EndIf

$CountLines = _FileCountLines("patient.txt")


For $i = 1 to $CountLines Step +1
$line1 = FileReadLine($file,$i)
If @error = -1 Then ExitLoop

While 1
    $line2 = FileReadLine($file)
    If @error = -1 Then ExitLoop
    If $line1 = $line2 then call ("_Cleanup")

Wend
Next

Func _Cleanup()
    
    $save = FileOpen("result.txt", 1)

; Check if file opened for writing OK
If $save = -1 Then
    MsgBox(0, "Error", "Unable to open file.")
    Exit
EndIf

FileWriteLine($save, $line2 & @CRLF)

FileClose($save)


EndFunc
$save2 = FileOpen("result.txt", 1)
FileWriteLine($save2, "ENDING" & @CRLF);<---------------Here's the change
FileClose($save2)
FileClose($file)
$filex = FileOpen("patient.txt", 1)
FileWriteLine($filex, "ENDING" & @CRLF);<---------------And Here, that's it
FileClose($filex)
;-----------
$file = FileOpen("result.txt", 0)
$filex = FileOpen("patient.txt", 0)
If $file = -1 Then
    
    Exit
EndIf

If $filex = -1 Then
    
    Exit
EndIf


$save = ""
; Read in lines of text until the EOF is reached
$CountLines = _FileCountLines("result.txt")


For $i = 1 to $CountLines Step +1
$line1 = FileReadLine($file,$i)
If @error = -1 Then ExitLoop
while 1
    $line = FileReadLine($filex)
    If @error = -1 Then ExitLoop
    If Not StringInStr($line, $line1)Then $save = $save & $line & @CRLF
    if $line = $line1 then ExitLoop
WEnd
Next




FileClose($file)
Fileclose($filex)
$end = FileOpen("patient.txt", 2)
FileWrite($end, $save)
Fileclose($end)
FileDelete("result.txt")

if anyone know whats going on, i am interested to hear about it. (End is not an function right?)

Link to comment
Share on other sites

  • Moderators

If you have Beta, this looks like what your trying to do:

#include <file.au3>

Local $File_Location_Name = @ScriptDir & '\Test.txt'
Local $nArray
If Not _FileReadToArray($File_Location_Name, $nArray) Then Exit

For $iCount = 0 To UBound($nArray) - 1
    If $iCount = UBound($nArray) - 1 Then ExitLoop
    If $nArray[$iCount] = $nArray[$iCount + 1] Then
        MsgBox(64, 'Information:', 'Line ' & $iCount & ' and Line ' & $iCount + 1 & ' are the same.')
    EndIf
Next

Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer.

Link to comment
Share on other sites

If you have Beta, this looks like what your trying to do:

#include <file.au3>

Local $File_Location_Name = @ScriptDir & '\Test.txt'
Local $nArray
If Not _FileReadToArray($File_Location_Name, $nArray) Then Exit

For $iCount = 0 To UBound($nArray) - 1
    If $iCount = UBound($nArray) - 1 Then ExitLoop
    If $nArray[$iCount] = $nArray[$iCount + 1] Then
        MsgBox(64, 'Information:', 'Line ' & $iCount & ' and Line ' & $iCount + 1 & ' are the same.')
    EndIf
Next
Man, your good. It took me 1,5 days to get mine working (and it's afwull big to) :D
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...