Compare 2 files for a match

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
Karis
Posts: 7
Joined: 08 Mar 2017, 11:22

Compare 2 files for a match

10 Mar 2017, 08:58

I spent hours trying to figure out what's wrong... with no succes.

What I'm trying to do:
Make script download 2 text files from 2 websites. Read them both, if there's a match, pop up a message box saying Match.

Code: Select all

UrlDownloadToFile, http://authority.000webhostapp.com/test, key
FileRead, key, key

UrlDownloadToFile, https://wtfismyip.com/text, ip
FileRead, ip, ip

if ip = %key%
{
MsgBox, Match
sleep 200
ExitApp
}
}
User avatar
TheDewd
Posts: 1510
Joined: 19 Dec 2013, 11:16
Location: USA

Re: Compare 2 files for a match

10 Mar 2017, 09:15

Very close... I think your issue is using percent signs in your var matching...

Code: Select all

#SingleInstance, Force

Gui, Margin, 10, 10
Gui, Add, Text, w220, Input:
Gui, Add, Edit, y+10 wp r1 vUser
Gui, Add, Button, y+10 wp h20 gBtnSubmit, Submit
Gui, Show, AutoSize, Authorization
return

BtnSubmit:
    Gui, Submit
    UrlDownloadToFile, http://authority.000webhostapp.com/test, key
    UrlDownloadToFile, https://wtfismyip.com/text, ip
    FileRead, key, key
    FileRead, ip, ip

    If (ip = key) {
        MsgBox, Match
        ExitApp
    }
return
Ovg
Posts: 23
Joined: 19 Feb 2017, 01:13

Re: Compare 2 files for a match

10 Mar 2017, 09:17

Extra } at the end of the file
Last edited by Ovg on 10 Mar 2017, 09:18, edited 1 time in total.
It's impossible to lead us astray for we don't care even to choose the way.
wolf_II
Posts: 2688
Joined: 08 Feb 2015, 20:55

Re: Compare 2 files for a match

10 Mar 2017, 09:18

Try this:

Code: Select all

Gui, Add, Edit, x12 y30 w220 h20 vUser,
Gui, Add, Button, x12 y60 w220 h20 , Submit
Gui, Add, Text, x12 y10 w90 h20 +BackgroundTrans, Input
; Generated using SmartGUI Creator for SciTE
Gui, Show, w256 h93, Authorization
return

ButtonSubmit:
    Gui, Submit

    UrlDownloadToFile, http://authority.000webhostapp.com/test, key
    FileRead, key, key

    UrlDownloadToFile, https://wtfismyip.com/text, ip
    FileRead, ip, ip

    if ip = %key%
    {
        MsgBox, Match
        ExitApp
    }

    else
        MsgBox, no match!`n`n%key%`n%ip%

Return
I hope that helps.
brutus_skywalker
Posts: 175
Joined: 24 Dec 2016, 13:16
Location: Antarctica

Re: Compare 2 files for a match

10 Mar 2017, 09:21

Just do a size comparison,as FileSize reads size in bytes unless defined otherwise,it should be very accurate.

Code: Select all

FileGetSize, keySize, key
FileGetSize, ipSize, ip

if ipSize = keySize
  {
 MsgBox Match
  ExitApp
  }
Outsourcing Clicks & Presses Since 2004.
User avatar
TheDewd
Posts: 1510
Joined: 19 Dec 2013, 11:16
Location: USA

Re: Compare 2 files for a match

10 Mar 2017, 09:27

brutus_skywalker wrote:Just do a size comparison,as FileSize reads size in bytes unless defined otherwise,it should be very accurate.
This would be the same as counting the characters. If both IP addresses had the same number of characters, it would read as a match even if the IP addresses didn't match. Not very accurate.
brutus_skywalker
Posts: 175
Joined: 24 Dec 2016, 13:16
Location: Antarctica

Re: Compare 2 files for a match

10 Mar 2017, 09:29

TheDewd wrote:
brutus_skywalker wrote:Just do a size comparison,as FileSize reads size in bytes unless defined otherwise,it should be very accurate.
This would be the same as counting the characters. If both IP addresses had the same number of characters, it would read as a match even if the IP addresses didn't match. Not very accurate.
Ahh,yeah, differences between files would have to be significant my approach to be remotely reliable.
So yeah disregard...
Outsourcing Clicks & Presses Since 2004.
Karis
Posts: 7
Joined: 08 Mar 2017, 11:22

Re: Compare 2 files for a match

10 Mar 2017, 10:38

TheDewd wrote:Very close... I think your issue is using percent signs in your var matching...

Code: Select all

#SingleInstance, Force

Gui, Margin, 10, 10
Gui, Add, Text, w220, Input:
Gui, Add, Edit, y+10 wp r1 vUser
Gui, Add, Button, y+10 wp h20 gBtnSubmit, Submit
Gui, Show, AutoSize, Authorization
return

BtnSubmit:
    Gui, Submit
    UrlDownloadToFile, http://authority.000webhostapp.com/test, key
    UrlDownloadToFile, https://wtfismyip.com/text, ip
    FileRead, key, key
    FileRead, ip, ip

    If (ip = key) {
        MsgBox, Match
        ExitApp
    }
return
Didn't work unfortunately.
wolf_II wrote:Gui, Add, Edit, x12 y30 w220 h20 vUser,
Gui, Add, Button, x12 y60 w220 h20 , Submit
Gui, Add, Text, x12 y10 w90 h20 +BackgroundTrans, Input
; Generated using SmartGUI Creator for SciTE
Gui, Show, w256 h93, Authorization
return

ButtonSubmit:
Gui, Submit

UrlDownloadToFile, http://authority.000webhostapp.com/test, key
FileRead, key, key

UrlDownloadToFile, https://wtfismyip.com/text, ip
FileRead, ip, ip

if ip = %key%
{
MsgBox, Match
ExitApp
}

else
MsgBox, no match!`n`n%key%`n%ip%

Return
Gave me a no match. But displayed the exact same IPs.
wolf_II
Posts: 2688
Joined: 08 Feb 2015, 20:55

Re: Compare 2 files for a match

10 Mar 2017, 12:27

Try this:

Code: Select all

Gui, Add, Edit, x12 y30 w220 h20 vUser,
Gui, Add, Button, x12 y60 w220 h20 , Submit
Gui, Add, Text, x12 y10 w90 h20 +BackgroundTrans, Input
; Generated using SmartGUI Creator for SciTE
Gui, Show, w256 h93, Authorization
return

ButtonSubmit:
    Gui, Submit

    UrlDownloadToFile, http://authority.000webhostapp.com/test, key
    FileRead, key, key

    UrlDownloadToFile, https://wtfismyip.com/text, ip
    FileRead, ip, ip

    if ip = %key%
    {
        MsgBox, Match
        ExitApp
    }

    else {
        Loop, % StrLen(key)
            If (k:=SubStr(key, A_Index, 1)) != (i:=SubStr(ip, A_Index, 1)) {
                p := A_Index
                Break
            }
        }
        MsgBox, no match!`n`n%key%`n%ip%`nmismatch at position %p%`n %k% != %i%
Return
I hope that helps.
Karis
Posts: 7
Joined: 08 Mar 2017, 11:22

Re: Compare 2 files for a match

10 Mar 2017, 12:31

Mismatched.

Displayed exact same IPs like before.
wolf_II
Posts: 2688
Joined: 08 Feb 2015, 20:55

Re: Compare 2 files for a match

10 Mar 2017, 12:37

Try this: (otherwise, please report where the mismatch position was found!)

Code: Select all

Gui, Add, Edit, x12 y30 w220 h20 vUser,
Gui, Add, Button, x12 y60 w220 h20 , Submit
Gui, Add, Text, x12 y10 w90 h20 +BackgroundTrans, Input
; Generated using SmartGUI Creator for SciTE
Gui, Show, w256 h93, Authorization
return

ButtonSubmit:
    Gui, Submit

    UrlDownloadToFile, http://authority.000webhostapp.com/test, key
    FileRead, key, key

    UrlDownloadToFile, https://wtfismyip.com/text, ip
    FileRead, ip, ip

    if Trim(ip) = Trim(key) ; trim whitespace
    {
        MsgBox, Match
        ExitApp
    }
Return
I hope that helps.
Karis
Posts: 7
Joined: 08 Mar 2017, 11:22

Re: Compare 2 files for a match

10 Mar 2017, 12:58

Mismatch position was found at 5 != 5 (previous script)

The one you just sent didn't give me anything after submitting.
Karis
Posts: 7
Joined: 08 Mar 2017, 11:22

Re: Compare 2 files for a match

11 Mar 2017, 16:27

Alright so it works if I put in the same link for "key" and "ip". Now what I don't understand is why the other link doesn't work, if it downloads the same exact file...
Karis
Posts: 7
Joined: 08 Mar 2017, 11:22

Re: Compare 2 files for a match

12 Mar 2017, 07:56

Retrieved the IP using the same website this time instead of wtfismyip. Still does not work. Is this a bug with autohotkey perhaps?


EDIT:
I might have fixed it! Using FileReadLine to read line 1 gave me a match. But I still do not understand why FileRead won't work.
Both downloaded files only have 1 line in them with the IP address. Then why is FileRead having so much trouble with it?

EDIT2:
It looks like it's still not functioning properly... This time whatever I put in shows up as a match.. As you can see the Msgbox when it fails is not displaying the second IP.

EDIT3:
It is not reading the downloaded file from the authority site properly for some reason..

EDIT4:
Used the wrong variable. My bad. It's working now, thank you everyone who helped.
brutus_skywalker
Posts: 175
Joined: 24 Dec 2016, 13:16
Location: Antarctica

Re: Compare 2 files for a match

12 Mar 2017, 13:14

final suggestion, instead of comparing strings or files, how about comparing the hashes of the downloaded files,here's a pretty good hashing function:

https://autohotkey.com/board/topic/8586 ... -md5-sha1/

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: Google [Bot], ulysim and 303 guests