Jump to content
Sign in to follow this  
dss

AutoIt masters - need assistance comparing and deleting

Recommended Posts

dss

I have very minimal knowledge of AutoIt. I come from basic/batch/vbscript/wisescript environment. I am learning AutoIt as I type this. I'm hoping someone can help me out / point me in the right direction. AutoIt has helped me in the past with many tasks. Again, I am a beginner here.

I'm looking for a script that compares two text files (contains listing of files) and sorting out the differences. Once the differences have been sorted out, I want to delete the files that are "different."

For example:

[file1.txt content]

c:dog.txt

c:cat.txt

c:mouse.txt

[file2.txt content]

c:dog.txt

c:cat.txt

c:mouse.txt

c:bear.txt

Logic:

Difference = c:bear.txt

Delete c:bear.txt

Any help would be appreciated.

-DS

Edited by dss

Share this post


Link to post
Share on other sites
Malkey

The following results are from testing the below script:-

Two 24KB text files with approx. 970 lines each, took 0.49 secs.

Comparing a 24KB text file with approx. 1010 lines

with a 30KB text file with approx. 1220 lines took 1.04 secs

Comparing a 24KB text file with approx. 1010 lines

with a 49KB text file with approx. 1940 lines took 3.37 secs

Comparing two 49KB text files with approx. 1940 lines each, did not work correctly on my xp.

"Comparing" meaning, creating an array containing lines that do not appear in both files.

;#cs
If FileExists("file1.txt") = 0 Then FileWrite("file1.txt", _
        "c:\ dog.txt" & @CRLF & _
        "c: \ cat.txt" & @CRLF & _
        "c:\mouse.txt" & @CRLF & _
        "c:\spider.txt")

If FileExists("file2.txt") = 0 Then FileWrite("file2.txt", _
        "c:\ pig.txt" & @CRLF & _
        "c:\ dog.txt" & @CRLF & _
        "c: \ cat.txt" & @CRLF & _
        "c:\horse.txt" & @CRLF & @CRLF & _
        "c:\mouse.txt" & @CRLF & _
        "c:\bear.txt")
;#ce

Local $begin = TimerInit()
Local $aDeleteList = _FileDiffences("file1.txt", "file2.txt", 0) ;
ConsoleWrite("Time taken: " & Round(TimerDiff($begin) / 1000, 4) & " secs" & @LF) ;

If IsArray($aDeleteList) Then
    ConsoleWrite("Deleting Files:" & @LF)
    For $i = 0 To UBound($aDeleteList) - 1
        ConsoleWrite($aDeleteList[$i] & @LF)
        If FileExists($aDeleteList[$i]) Then FileDelete($aDeleteList[$i])
    Next
EndIf

FileDelete("file1.txt")
FileDelete("file2.txt")


; Description: Compares two files by lines and returns the lines that do not exist in both files.
; Returns:- Array of the lines that do not exist in both files; or,
;          Zero when both files are the same.
Func _FileDiffences($file1, $file2, $iCaseSensitive = 0)
    Local $sCS = ""
    If ($iCaseSensitive = 0) Then $sCS = "(?i)"
    Local $sFile1Contents = StringRegExpReplace(FileRead($file1), "(\R+)", @CRLF) ; Remove blank lines
    Local $sFile2Contents = StringRegExpReplace(FileRead($file2), "(\R+)", @CRLF) ; Remove blank lines
    ;ConsoleWrite($sFile2Contents & @LF)
    If (($iCaseSensitive <> 0) And ($sFile1Contents = $sFile2Contents)) Or (($iCaseSensitive = 0) And ($sFile1Contents == $sFile2Contents)) Then Return 0

    ; Create part of Reg Exp pattern
    Local $sFile1REs = "(?:" & StringRegExpReplace(StringRegExpReplace(StringStripWS($sFile1Contents, 2), '([\^\\\[\]\-\|\.\(\)\?\*\+\{\}\$''])', "\\$1"), "(\v+)", ")|(?:") & ")"
    Local $sFile2REs = "(?:" & StringRegExpReplace(StringRegExpReplace(StringStripWS($sFile2Contents, 2), '([\^\\\[\]\-\|\.\(\)\?\*\+\{\}\$''])', "\\$1"), "(\v+)", ")|(?:") & ")"
    ;ConsoleWrite($sFile1REs & @LF)
    ;ConsoleWrite($sFile2REs & @LF)

    Local $sNonMatchingLines = StringStripWS(StringRegExpReplace($sFile2Contents & @CRLF, $sCS & "((" & $sFile1REs & ")(?:\R+))", ""), 7) & @CRLF & _
            StringRegExpReplace($sFile1Contents & @CRLF, $sCS & "((" & $sFile2REs & ")(?:\R+))", "")
    ;ConsoleWrite($sNonMatchingLines & @LF)

    Return StringRegExp($sNonMatchingLines, "([^\v]+)", 3)
EndFunc   ;==>_FileDiffences

Share this post


Link to post
Share on other sites
AZJIO

$Path_In1 = @ScriptDir & 'test_in1.txt'
$Path_In2 = @ScriptDir & 'test_in2.txt'
$Path_Out = @ScriptDir & 'test_Out.txt'
$sText1 = FileRead($Path_In1)
$sText2 = FileRead($Path_In2)

$sText_Out = _Alike_Lines($sText1, $sText2)
If @error Then
    MsgBox(0, 'Error', 'Error = ' & @error)
    Exit
Else
    $hFile = FileOpen($Path_Out, 2) ; пишем в файл
    FileWrite($hFile, $sText_Out)
    FileClose($hFile)
EndIf

; @error = 2 - Not found
; @error = 2 - Не найдено
; не учитывает регистр String = StRiNg = STRING
; not case sensitive, String = StRiNg = STRING
Func _Alike_Lines($sText1, $sText2, $sep = @CRLF)
    Local $i, $k, $aText, $s, $Trg = 0, $LenSep

    If StringInStr($sText1 & $sText2, '[') And $sep <> '[' Then ; если сбойный символ есть до заменяем его
        For $i = 0 To 255
            $s = Chr($i)
            If Not StringInStr($sText1 & $sText2, $s) Then
                If StringInStr($sep, $s) Then ContinueLoop
                $sText1 = StringReplace($sText1, '[', $s)
                $sText2 = StringReplace($sText2, '[', $s)
                $Trg = 1
                ExitLoop
            EndIf
        Next
        If Not $Trg Then Return SetError(1, 0, '')
    EndIf

    $LenSep = StringLen($sep)

    $aText = StringSplit($sText1, $sep, 1) ; Создаём переменные первого файла
    For $i = 1 To $aText[0]
        Assign($aText[$i] & '/', -2, 1)
    Next
    Assign('/', 2, 1)

    $aText = StringSplit($sText2, $sep, 1)

    $k = 0
    $sText1 = ''
    For $i = 1 To $aText[0]
        Assign($aText[$i] & '/', Eval($aText[$i] & '/') + 1, 1) ; создаём локальные переменные или увеличиваем значение для уже созданных
        If Eval($aText[$i] & '/') = -1 Then
            $sText1 &= $aText[$i] & $sep
            $k += 1
        EndIf
    Next
    If $k = 0 Then Return SetError(2, 0, '')
    If $Trg Then $sText1 = StringReplace($sText1, $s, '[')
    Return StringTrimRight($sText1, $LenSep)
EndFunc

If I did not understand, here's another option

$Path_In1 = @ScriptDir & 'test_in1.txt'
$Path_In2 = @ScriptDir & 'test_in2.txt'
$Path_Out = @ScriptDir & 'test_Out.txt'
$sText1 = FileRead($Path_In1)
$sText2 = FileRead($Path_In2)

$sText_Out = _Unique_Lines_Text2($sText1, $sText2)
If @error Then
    MsgBox(0, 'Error', 'Error = ' & @error)
    Exit
Else
    $hFile = FileOpen($Path_Out, 2) ; пишем в файл
    FileWrite($hFile, $sText_Out)
    FileClose($hFile)
EndIf

; @error = 2 - Not found
; @error = 2 - Не найдено
; не учитывает регистр String = StRiNg = STRING
; not case sensitive, String = StRiNg = STRING
Func _Unique_Lines_Text2($sText1, $sText2, $sep = @CRLF)
    Local $i, $k, $aText, $s, $Trg = 0, $LenSep

    If StringInStr($sText1 & $sText2, '[') And $sep <> '[' Then ; если сбойный символ есть до заменяем его
        For $i = 0 To 255
            $s = Chr($i)
            If Not StringInStr($sText1 & $sText2, $s) Then
                If StringInStr($sep, $s) Then ContinueLoop
                $sText1 = StringReplace($sText1, '[', $s)
                $sText2 = StringReplace($sText2, '[', $s)
                $Trg = 1
                ExitLoop
            EndIf
        Next
        If Not $Trg Then Return SetError(1, 0, '')
    EndIf

    $LenSep = StringLen($sep)

    $aText = StringSplit($sText1, $sep, 1) ; Создаём переменные первого файла
    For $i = 1 To $aText[0]
        Assign($aText[$i] & '/', 2, 1)
    Next
    Assign('/', 2, 1)

    $aText = StringSplit($sText2, $sep, 1)

    $k = 0
    $sText1 = ''
    For $i = 1 To $aText[0]
        Assign($aText[$i] & '/', Eval($aText[$i] & '/')+1, 1) ; создаём локальные переменные или увеличиваем значение для уже созданных
        If Eval($aText[$i] & '/') = 1 Then
            $sText1 &= $aText[$i] & $sep
            $k += 1
        EndIf
    Next
    If $k = 0 Then Return SetError(2, 0, '')
    If $Trg Then $sText1 = StringReplace($sText1, $s, '[')
    Return StringTrimRight($sText1, $LenSep)
EndFunc
Edited by AZJIO

Share this post


Link to post
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
Sign in to follow this  

  • Similar Content

    • Epic007
      By Epic007
      Hey guys I'm stuck with a program.....I need a make an automation which copies text from a chat bot app and paste it in notepad to log the replies of my chat bot and to analyse stuff....a lil help pls
       
       
       
       
       
       
       
      Thanks in advance
    • nooneclose
      By nooneclose
      I need to send a string of text to this popup and click on the ok button to save it.
      Here is the code I have so far:
      ;Start IE Sleep(7000) $oIE = _IECreate("http://www.google.com") Sleep(500) _IELoadWait($oIE) $hIE = _IEPropertyGet($oIE, "hwnd") ; Get Handle of the IE window Sleep(500) WinSetState($hIE, "", @SW_MAXIMIZE) ;Wait for a browser page load to complete Sleep(3000) _IENavigate($oIE, "https://properURL.com") Sleep(8000) _IELoadWait($oIE) ;Attach to a browser control embedded in another window $oIE = _IEAttach("https://"properURL.com", url") ConsoleWrite('@@ Debug(' & @ScriptLineNumber & ') : $oIE = ' & $oIE & @CRLF & '>Error code: ' & @error & ' Extended code: 0x' & Hex(@extended) & @CRLF) ;### Debug Console Sleep(2000) ;Get the title of the webpage ;Local $wTitle = _IEPropertyGet($oIE, "title") ;MsgBox($MB_SYSTEMMODAL, "Webpage title:", $wTitle) ;Clicks the new button Sleep(3000) _IEAction($nWorkOrderB, "focus") _IEAction($nWorkOrderB, "click") Sleep(5000) ;Store the Element names where the important data will be sent ;Store the long description button Local $wLongDButton = _IEGetObjById($oIE, "m65d795a4-img") ;Store the long Description field id Local $wComments = _IEGetObjById($oIE, "ma6499a9c-rte_iframe") ;Store the ok button id that is in the long description Local $wCommOk = _IEGetObjById($oIE, "m74031266-pb") ;******************************************************************************* ; Send the stored data to the proper field ;******************************************************************************* ;Click the long description button Sleep(300) _IEAction($wLongDButton, "focus") _IEAction($wLongDButton, "click") Sleep(300) ;Sends the Comments Sleep(500) _IEAction($wComments, "focus") _IEAction($wComments, "click") Sleep(500) _IEFormElementSetValue($wComments, "hello darkness my old friend") ;Click the ok button Sleep(500) _IEAction($wCommOk, "focus") _IEAction($wCommOk, "click") Sleep(500)  
      Here is the popup:

    • reida
      By reida
      Hello. Help to write the text.
      <textarea tabindex="4" class="js-input compose__labels__input" style="width: 407px; max-width: 413px;" wrap="off" autocomplete="off" data-original-name="To"></textarea>  I can not make.
      #include <IE.au3> $sLogin = 'adv' $iError = 1 $sUrl = 'https://......' $oIE = _IECreate($sUrl) ;;; _IELoadWait($oIE) $oLogin = _IEGetObjByName($oIE, "To") Sleep(7000) If IsObj($oLogin) Then _IEFormElementSetValue($oLogin, $sLogin) _IELoadWait($oIE) ; EndIf EndIf Through the selector it can be possible?
      #toolkit-154163367424641composeForm > div:nth-child(1) > div > div.compose-head__content.js-collapsed-off > div.compose-head__row-wrapper.compose-head__row-wrapper_to.js-row > div > div > div:nth-child(2) > div > div > div > textarea.js-input.compose__labels__input  
    • nooneclose
      By nooneclose
      My program has to first search for names in Column D that do not match up with column C. I got that search to work using arrays. It was slow and I could not figure out how to delete them so I just manually put coded the names that do not belong. I found their cell location but I do not know how to store that location and delete it.
      This is what I have so far.
      Local $NameToDelete1[6]  = _Excel_RangeFind($OpenWorkbook, "Smith, Bill") _ArrayDisplay($NameToDelete1, "Excel UDF: _Excel_RangeFind Example 1", "", 0, "|", "Sheet|Name|Cell|Value|Formula|Comment") _Excel_RangeDelete($OpenWorkbook.ActiveSheet, $NameToDelete1[2], $xlShiftUp)  
      Please help, I wanted to have this program done yesterday but I did not see this problem until yesterday. 
    • cramaboule
      By cramaboule
      Hi all,
       
      I was wondering if it is possible (and how) to put text on video on-the-fly (like VLC displays Title movie at the begining)
      and of course doing that in AutoIt
      Thanks in advance...
       
      C.
×