Jump to content

Recommended Posts

Posted

I need a simple script in autoit that can open two text files and compare numbers withing them and decide which one is bigger for an update program im creating.

Thanks

Jamie


Posted

Some use this method. This will work so long as the versions are not padded with leading zeros.


If Not FileExists('file1.txt') And Not FileExists('file2.txt') Then
; create test files
If FileWrite('file1.txt', '2.1.1.0') And FileWrite('file2.txt', '1.1.1.0') Then
ConsoleWrite('Test files written' & @CRLF)
EndIf

; Read test files and compare versions
$file1 = StringStripWS(FileRead('file1.txt'), 3)
$file2 = StringStripWS(FileRead('file2.txt'), 3)
If $file1 > $file2 Then
MsgBox(0x40000, '', $file1 & ' > ' & $file2 & @CRLF)
EndIf

; remove test files
If FileDelete('file1.txt') And FileDelete('file2.txt') Then
ConsoleWrite('Test files removed' & @CRLF)
EndIf
EndIf

To have a more thorough method then use StringSplit to split the version into elements that can be compared numerically.


If Not FileExists('file1.txt') And Not FileExists('file2.txt') Then
; create test files
If FileWrite('file1.txt', '2.1.1.0') And FileWrite('file2.txt', '2.2.1.0') Then
ConsoleWrite('Test files written' & @CRLF)
EndIf

; Read test files and compare versions
$file1 = FileRead('file1.txt')
$file2 = FileRead('file2.txt')
If _HigherVersion($file1, $file2) And Not @error Then
MsgBox(0x40000, '', 'Higher version')
ElseIf @error Then
MsgBox(0x40000, 'Error', @error)
EndIf

; remove test files
If FileDelete('file1.txt') And FileDelete('file2.txt') Then
ConsoleWrite('Test files removed' & @CRLF)
EndIf
EndIf

Func _HigherVersion($current, $update)
$current = StringSplit($current, '.')
If @error Then Return SetError(1, 0, '')
$update = StringSplit($update, '.')
If @error Then Return SetError(1, 0, '')
If $current[0] <> $update[0] Then Return SetError(2, 0, '')
For $i = 1 To $current[0]
If Int($current[$i]) = Int($update[$i]) Then
ContinueLoop
ElseIf Int($current[$i]) < Int($update[$i]) Then
Return True
EndIf
Next
EndFunc

:)

Posted

Those are both fantastic thank you very much.

They have helped me alot. i hope to release a program soon because of your help

:thumbup:thumbup

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
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...