Jump to content
Sign in to follow this  
rizal

compare 2 value from different files

Recommended Posts

rizal

i have 2 txt files [file1.txt & file2.txt]. each file have different value[integer] stored.

i want to compare. if the value in file1.txt > file2.txt, a message box will appear and show "Pass". if not, it will show "failed".

Please help me solve this. I'm still new to autoit...

Share this post


Link to post
Share on other sites
Melba23

rizal,

The short answer is: Read the 2 files into variables using FileRead/_FileReadToArray and then do some comparing. ;)

The longer answer is:

- Where is this integer stored within the file? In a specific line? If so you could use FileReadLine to reduce the size of the strings to compare.

- Are the integers of the same or different lengths? If the same, a simple StringMid should suffice to do the comparison - if not you will probably have to look at _StringBetween or a RegExp. :)

Perhaps if you posted a couple of sample files indicating where the integers are within them we could be more specific. ;)

M23


Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind._______My UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Share this post


Link to post
Share on other sites
rizal

file1 & file2 only stored integer value and just 1 line. I include the txt file. the integer length is different. the txt file is stored in D:\

After compare the value, i want a message box to appear to tell that file1 value is bigger than file2.

thanks

sample.rar

Share this post


Link to post
Share on other sites
987learner

Hi Rizal

I'm a newbie here too.

Some simple code to get you going. No extensive error checking code here.

$sFile1="file1.txt"
$sFile2="file2.txt"

$hfile1=FileOpen ( $sFile1, 0 )
If $sFile1 = -1 Then
    MsgBox(0, "Error", "Unable to open "&$sFile1&" file")
    Exit
EndIf
$ivalue1a=Fileread ($hfile1);file 1 handle
FileClose($sFile1)
msgbox (0,"File1",$ivalue1a)     ;test point, remove this line after tested ok

$hfile2=FileOpen ( $sFile2, 0 )
If $sFile2 = -1 Then
    MsgBox(0, "Error", "Unable to open "&$sFile2&" file")
    Exit
EndIf
$ivalue2a=Fileread ($hfile2)
FileClose($sFile2)
msgbox (0,"File1",$ivalue2a)     ;test point

If $ivalue1a > $ivalue2a then msgbox (0,"Output",$sFile1 & " has larger integer"&@crlf& _
$sFile1 & " has "& $ivalue1a&@crlf& _
$sFile2 & " has "& $ivalue2a)

Share this post


Link to post
Share on other sites
rizal

Hi Rizal

I'm a newbie here too.

Some simple code to get you going. No extensive error checking code here.

$sFile1="file1.txt"
$sFile2="file2.txt"

$hfile1=FileOpen ( $sFile1, 0 )
If $sFile1 = -1 Then
    MsgBox(0, "Error", "Unable to open "&$sFile1&" file")
    Exit
EndIf
$ivalue1a=Fileread ($hfile1);file 1 handle
FileClose($sFile1)
msgbox (0,"File1",$ivalue1a)     ;test point, remove this line after tested ok

$hfile2=FileOpen ( $sFile2, 0 )
If $sFile2 = -1 Then
    MsgBox(0, "Error", "Unable to open "&$sFile2&" file")
    Exit
EndIf
$ivalue2a=Fileread ($hfile2)
FileClose($sFile2)
msgbox (0,"File1",$ivalue2a)     ;test point

If $ivalue1a > $ivalue2a then msgbox (0,"Output",$sFile1 & " has larger integer"&@crlf& _
$sFile1 & " has "& $ivalue1a&@crlf& _
$sFile2 & " has "& $ivalue2a)

i try it but it only show the message box that show the value of the file1 & file2.

it don't show the message box that file1 value is > than file2 value.

how to make it appear?

Share this post


Link to post
Share on other sites
GEOSoft

This is just written on the fly and not tested but it should be fine or at least show you one method of doing it

$sStr = FileRead("file1.txt") & @CRLF & FileRead("file2.txt")
$aValues = StringRegExp($sStr, "(?m:^).*?(\d+).*(?:\v|$)+", 3)
If NOT @Error Then
    If UBound($aValues) < 2 Then
        MsgBox(4096, "Error",  "The files do not both contain digits")
    Else
        If Int($aValues[0]) = Int($aValues[1]) Then
            MsgBox(0, "Result", "The values are the same")
        ElseIf Int($aValues[0]) > Int($aValues[1]) Then
            MsgBox(0, "Result", "The value in File1.txt is larger")
        Else
            MsgBox(0, "Result", "The value in File2.txt is larger")
        EndIf
    EndIf
EndIf

EDIT: Try not to upload RAR files. I won't even be bothered downloading them because then I have to extract them to view the files. Windows has native zip support and I can just open the file without extracting it. Not a problem extracting but it's an extra step I won't be bothered with.

Edited by GEOSoft

George

Question about decompiling code? Read the decompiling FAQ and don't bother posting the question in the forums.

Be sure to read and follow the forum rules. -AKA the AutoIt Reading and Comprehension Skills test.***

The PCRE (Regular Expression) ToolKit for AutoIT - (Updated Oct 20, 2011 ver:3.0.1.13) - Please update your current version before filing any bug reports. The installer now includes both 32 and 64 bit versions. No change in version number.

Visit my Blog .. currently not active but it will soon be resplendent with news and views. Also please remove any links you may have to my website. it is soon to be closed and replaced with something else.

"Old age and treachery will always overcome youth and skill!"

Share this post


Link to post
Share on other sites
rizal

thanks to GEOSoft. i get the expected output. thanks again..

Share this post


Link to post
Share on other sites
987learner

i try it but it only show the message box that show the value of the file1 & file2.

it don't show the message box that file1 value is > than file2 value.

how to make it appear?

Sorry for the late reply as I was out for 8 hrs after I post it. I retest again, no problem here. Can appear as intended. Don't really know why it is not working at your end.

Nevertheless, GEOSoft has provided you a sample, but for learning sake, have you benefit from his script?

Do bear in mind we assumed in good faith your file1.txt & file2.txt will always contain integer value only. (I believe you extact the value thru some other script/batch file)

The problem is: if file1.txt has a value of 5w8780 then output could evaluate wrongly.

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  

×