Sign in to follow this  
Followers 0
dss

AutoIt masters - need assistance comparing and deleting

4 posts in this topic

#1 ·  Posted (edited)

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



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

#4 ·  Posted (edited)

$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  
Followers 0

  • Similar Content

    • houser747
      By houser747
      I have previously used _IEFormElementGetObjByName and _IEFormElementSetValue to enter text into a search box on a form and then submit the form.
      I am now trying to enter text into a search box which is not part of a form. 
      Here is the HTML from the website that i'm trying to enter the data on and then submit the search.
      <div class="row">
          <div class="form-group col-xs-12">
              <span id="FullWidthWithSubmenuContent_FullWidthContent_MainContent_AircraftRegistry_lblSearchText" for="input-search">Registreringsbeteckning</span>
              <div class="input-group col-xs-12">
                  <span id="FullWidthWithSubmenuContent_FullWidthContent_MainContent_AircraftRegistry_preSearchText" class="input-group-addon">SE -</span>
                  <input name="ctl00$FullWidthWithSubmenuContent$FullWidthContent$MainContent$AircraftRegistry$txtSearchText" type="text" value="DTH" id="FullWidthWithSubmenuContent_FullWidthContent_MainContent_AircraftRegistry_txtSearchText" class="form-control" />
              </div>
          </div>
      </div>
      <div class="row">
          <div class="form-group col-xs-12">
              <label class="sr-only" for="">Sök</label>
              <input type="submit" name="ctl00$FullWidthWithSubmenuContent$FullWidthContent$MainContent$AircraftRegistry$btnSearch" value="Sök" id="FullWidthWithSubmenuContent_FullWidthContent_MainContent_AircraftRegistry_btnSearch" class="btn btn-primary ladda-button" data-style="expand-right" />
          </div>
      </div>
      Many thanks in advance
      cheers
      Roger
    • czardas
      By czardas
      Haven't had much time to code recently. However the following thread inspired me.
      The debate about linear, parallel and binary search methods was rather interesting and, in an attempt to be diplomatic, I decided to combine @jchd's suggestion with @LarsJ's binary search example. I decided that the binary search algorithm required modification to make it more linear. As usual, 'if you invent something, it probably already exists and if it already exists, it exists for a reason'. My first attempt was not all that good. The code worked but was really a mess. I blame peer pressure (to post an example of a parallel search method).  I will delete that old code in due course.
      With a little memory jogging and a glance at the help file, the solution turned out to be quite easy: I just needed a better understanding of Euler. Further modification will be needed to work with more complicated unicode strings. The output could be returned as an array or a delimitered string. I'm not so interested in those details. I'm just going to post the algorithm for now and anyone, who wants to, can modify it to suit their needs. Both arrays must contain at least 1 element.
      Local $aFoo = [0,1,2,3,4,5,6,7,9,10,11,12,13,14,15,16,19,20,23,24,26,30,35,39,40,41] Local $aBar = [0,1,5,6,7,8,9,10,11,12,13,14,17,18,19,21,24,25,26,27,34,35,38,40] ParallelExponetialSearch($aFoo, $aBar) ; Compares two lists - returning positive matches. Each input array must be unique (individually) and in alphabetical order. Func ParallelExponetialSearch($aFoo, $aBar) Local $sFind, _ $iMin_F = -1, $iMax_F = UBound($aFoo) -1, $Lo_F = $iMin_F, $Hi_F, _ $iMin_B = -1, $iMax_B = UBound($aBar) -1, $Lo_B = $iMin_B, $Hi_B While $iMin_F < $iMax_F And $iMin_B < $iMax_B ; Toggle Arrays - Which array has most untested elements? This is the one we want to search next, ; so we can bypass more comparisons because (in theory) mismatches have a greater chance of being skipped. If $iMax_F - $iMin_F >= $iMax_B - $iMin_B Then ; $aFoo has more (or an equal number of) untested elements $Hi_F = $iMax_F $iMin_B += 1 $sFind = $aBar[$iMin_B] While $Lo_F < $Hi_F ; search $aFoo For $i = 0 To Floor(Log($Hi_F - $Lo_F) / Log(2)) $Lo_F = $iMin_F + 2^$i If $aFoo[$Lo_F] = $sFind Then $iMin_F = $Lo_F ; each match should be added to the output [perhaps an array] ConsoleWrite($sFind & " found at $aFoo[" & $Lo_F & "] = $aBar[" & $iMin_B & "]" & @LF) ExitLoop 2 ElseIf $aFoo[$Lo_F] > $sFind Then $Hi_F = $Lo_F -1 $iMin_F += Floor(2^($i -1)) $Lo_F = $iMin_F ContinueLoop 2 EndIf Next $iMin_F = $Lo_F ; minimum increment is one WEnd Else ; $aBar has more untested elements $Hi_B = $iMax_B $iMin_F += 1 $sFind = $aFoo[$iMin_F] While $Lo_B < $Hi_B ; search $aBar For $i = 0 To Floor(Log($Hi_B - $Lo_B) / Log(2)) $Lo_B = $iMin_B + 2^$i If $aBar[$Lo_B] = $sFind Then $iMin_B = $Lo_B ; each match should be added to the output [perhaps an array] ConsoleWrite($sFind & " found at $aFoo[" & $iMin_F & "] = $aBar[" & $Lo_B & "]" & @LF) ExitLoop 2 ElseIf $aBar[$Lo_B] > $sFind Then $Hi_B = $Lo_B -1 $iMin_B += Floor(2^($i -1)) $Lo_B = $iMin_B ContinueLoop 2 EndIf Next $iMin_B = $Lo_B ; minimum increment is one WEnd EndIf WEnd EndFunc ;==> ParallelExponetialSearch I hope this will be useful to someone. I believe it deserved a thread of its own!
    • robcull
      By robcull
      Hello all! I have had some issues reading text from different types of windows, occasionally, specifically with controlgettext. 
      **Before I begin, I know there are better ways to do what I attempt in the example below. That's not the point of this post. The point is my issues with controlgettext. 
      I am about to cite an example with an application you may be familiar with called SpeedFan (v4.52). My problem is not specific to speedfan, it is simply the most recent and easily reproducible example I can think of. 
      So, the goal of the script below is to get a string of text containing the current fan RPMs from the highlighted control in the screenshot below (see "speedfan_control_details.png").

      Now, here's a simple script for grabbing the window handle and reading the text from that control: 
      $wintitle = "SpeedFan 4.52" $controlID = "197934" ;will be reformatted as "[ID:######]" $hwnd = wingethandle($wintitle) if @error<>0 then msgbox(0, "WinGetHandle", "FAILURE. @error="&@error) Exit EndIf $text = ControlGetText($hwnd, "", "[ID:"&$controlID&"]") if @error=1 then msgbox(0, "ControlGetText", "FAILURE. @error="&@error) ;failure returns "" and @error=1 Exit EndIf msgbox (0, "ControlGetText", "SUCCESS. @error="&@error &@CRLF& "$text="&$text) ;success returns string and @error=0 You'll see that the ControlGetText operation runs without error, however it does not capture any text from the control. If you explore the other controls in this one window, you'll find mixed results across the board. Neither the temps nor voltages can be read, while the log field and some other elements can be read. Even when you read the text from the whole window, those elements are not included in the visible nor hidden texts. 
       
      I have run into this issue many times in the past- inconsistencies in the ability of autoit to interact with certain controls. What is it which makes this text different than any other readable texts? Is there an alternate method of reading the text in the window/control which could work? Any and all info to help me solve this mystery and satisfy my curiosity would be greatly appreciated. 
      Thanks  -Rob C
      PS: Running Autoit v3.3.14.2 on Win7 Ultimate x64
    • thoms
      By thoms
      Hello Forum,
      I'm trying to vertically center text in label controls, but no way. The search on the forum returns no result, or I don't search the right way
      When I insert a button and a label of same size close to each other, the text is centered on the button, but top aligned on the label, or edit. Which doesn't look really aesthetic
      Any idea is welcome
      Thanks in advance,
      Thoms
    • nacerbaaziz
      By nacerbaaziz
      Hello my friends
      I am a totally blind young man
      for easier to read books and articles Among the blind
      i has programmed a tool
      This tool converts text to audio with the possibility to save it into a wav or mp3 file
      It is also compatible with all persons, whether blind or ordinary
      I have completed the work of this tool and want to take your opinion
      i Especially published here for anyone looking about how to convert text to speech, I will put this tool open source for you
      I want to know what your think about it
      and if it need any other additions?
      Note :
      This tool converts texts to audio using sapi 5 voices
      so i was also puted a hot keys to control the tool
      Now I put this project to you for public benefit and I am ready to answer any question.
      Apology :
      I am an Arab youth from Algeria
      I do not mastered English very well
      So I apologize to you if there are written mistakes
      With my greetings and best wishes
      My texts to audio converter.zip