$sFile = @DesktopDir & "\Test.txt"
#Region Create file (30+- kb)
$sData = ""
$iRandom1 = Random(1, 50, 1)
$iRandom2 = Random(1, 50, 1)
For $i = 1 To 1024*5 ;1024*1024 ;7+ mb
$sData &= $i & @CRLF
If $i = $iRandom1 Or $i = $iRandom2 Then
$sData &= $i & @CRLF
ConsoleWrite($i & @LF) ;Display the duplicates
EndIf
Next
$hFile = FileOpen($sFile, 2)
FileWrite($hFile, $sData)
FileClose($hFile)
#EndRegion Create file (30+- kb)
$Ret = _FileDeleteDuplicates($sFile, 1)
If $Ret = 1 Then
MsgBox(64, "Results", "Was deleted <" & @extended & "> duplicates in file [" & $sFile & "]")
ElseIf @error Then
MsgBox(48, "Error", "File [" & $sFile & "] was not found.")
Else
MsgBox(64, "Results", "No duplicates found.")
EndIf
Func _FileDeleteDuplicates($sFile, $sDirection=-1)
If Not FileExists($sFile) Or StringInStr(FileGetAttrib($sFile), "D") Then Return SetError(1, 0, 0)
Local $FileStr = FileRead($sFile), $FileHasEndCRLF = True, $Extended = 0
Local $ReplDirectVal = '\1\2'
If $sDirection <> -1 Then $ReplDirectVal = '\2\1'
If StringRight($FileStr, 2) <> @CRLF Then
$FileStr &= @CRLF
$FileHasEndCRLF = False
EndIf
Do
$FileStr = StringRegExpReplace($FileStr, '(?sm)(^[^\r\n]+\r\n?)(.*?)^\1', $ReplDirectVal)
$Extended += @extended
Until @extended = 0
If $Extended = 0 Then Return SetExtended(0, 0)
If Not $FileHasEndCRLF Then $FileStr = StringTrimRight($FileStr, 2)
Local $hFile = FileOpen($sFile, 2)
FileWrite($hFile, $FileStr)
FileClose($hFile)
Return SetExtended($Extended/2, 1)
EndFunc