QB64 Phoenix Edition
Pete's handy dandy compare tool... - Printable Version

+- QB64 Phoenix Edition (https://qb64phoenix.com/forum)
+-- Forum: QB64 Rising (https://qb64phoenix.com/forum/forumdisplay.php?fid=1)
+--- Forum: Code and Stuff (https://qb64phoenix.com/forum/forumdisplay.php?fid=3)
+---- Forum: Utilities (https://qb64phoenix.com/forum/forumdisplay.php?fid=8)
+---- Thread: Pete's handy dandy compare tool... (/showthread.php?tid=3160)



Pete's handy dandy compare tool... - Pete - 10-25-2024

Maybe Steve already posted one of these, and he codes more efficiently, so I'm sure my tool is bigger than his tool, but...

Code: (Select All)
_Title "File Compare"

' Copy to the clipboard for each of the (4) prompts that appear one at a time.
' Note for .bas files you do not need to copy the .bas extension.
' The routine will inform you of any copy errors and let you retry.
' Esc        Quit
' Backspace  Redo previous prompt
' Enter      Inputs the current directory.
' Tab        Can be used when directory or file names are the same, instead of doing another copy.
' Spacebar  Change to a type-in input line.

' Input complete...
' If the files are identical, it will instantly inform you.
' If the files are different, it will display both codes, line by line and pause with a "No Match!" alert were the code is different.
' When paused you can...
' F1        Copy to the clipboard the no match line (last line shown) of the 1st file.
' F2        Copy to the clipboard the no match line (last line shown) of the 2nd file.
'            Note: Copying the line and paste it into an IDE search to find the code in your program.
' 1          Scroll to the next line of the 1st file.
' 2          Scroll to the next line of the 2nd file.
' Spacebar  Toggle to pause/resume code while it is scrolling.
' Enter      Auto continue (scroll both) to the next no match.
' Esc        Quit

Width 70, 20
_Font 16
_ScreenMove 20, 0
v1top = 2: v1btm = 20
v2top = 22: v2btm = 40
View Print
Cls
If Len(_Clipboard$) > 100 Then
    Print "Warning: This application will erase your current clipboard contents."
    Print: Print "Press Enter to continue or Esc to quit..."
    Do
        b$ = InKey$
        If Len(b$) Then
            If b$ = Chr$(27) Then System
            If b$ = Chr$(13) Then Exit Do
        End If
        _Limit 60
    Loop
    _Clipboard$ = Chr$(0)
Else
    hold$ = _Clipboard$
    _Clipboard$ = Chr$(0)
End If
Cls
Do
    redo1:
    While -1
        Locate 1, 1: View Print CsrLin To _Height - 1: Cls 2: View Print
        Locate 1, 1: Print "Paste first directory name: ": Locate 3, 1, 1, 7, 30
        Do
            _Limit 60
            b$ = InKey$
            If Len(b$) Then
                _Limit 10
                Select Case b$
                    Case Chr$(27): System
                    Case Chr$(13): dir1$ = _CWD$: Exit Do
                    Case Chr$(32): GoSub manual: If x$ = "" Then redo = 1: b$ = Chr$(8): Exit While
                End Select
            End If
            If Len(_Clipboard$) Then GoSub clip: dir1$ = x$: Exit Do
        Loop
        _Clipboard$ = Chr$(0)
        If Right$(dir1$, 1) <> "\" Then dir1$ = dir1$ + "\"
        Locate 3, 1: Print dir1$
        If _DirExists(dir1$) Then Exit While Else Print "Directory not found: "; dir1$; " Any key to redo...";: Sleep
    Wend
    If b$ = Chr$(8) Then Exit Do
    redo2:
    While -1
        Locate 5, 1: View Print CsrLin To _Height - 1: Cls 2: View Print
        Locate 5, 1: Print "Paste first file name: ": Locate 7, 1
        If Len(filehold$) Then
            file1$ = filehold$: filehold$ = ""
        Else
            Do
                _Limit 60
                b$ = InKey$
                If Len(b$) Then
                    Select Case b$
                        Case Chr$(27): System
                        Case Chr$(8): redo = 1: Exit While
                        Case Chr$(32): GoSub manual: If x$ = "" Then redo = 2: b$ = Chr$(8): Exit While
                    End Select
                End If
                If Len(_Clipboard$) Then GoSub clip: file1$ = x$: Exit Do
            Loop
        End If
        _Clipboard$ = Chr$(0)
        If InStr(file1$, ".") = 0 Then file1$ = file1$ + ".bas"
        Print file1$
        If _FileExists(dir1$ + file1$) Then Exit While Else Print "File not found: "; dir1$ + file1$; " Any key to redo...";: Sleep
    Wend
    If b$ = Chr$(8) Then Exit Do
    redo3:
    While -1
        Locate 9, 1: View Print CsrLin To _Height - 1: Cls 2: View Print
        Locate 9, 1: Print "Paste second directory name: ": Locate 11, 1
        Do
            _Limit 60
            b$ = InKey$
            If Len(b$) Then
                Select Case b$
                    Case Chr$(27): System
                    Case Chr$(8): redo = 2: Exit While
                    Case Chr$(13): dir2$ = _CWD$: Exit Do
                    Case Chr$(9): dir2$ = dir1$: Exit Do
                    Case Chr$(32): GoSub manual: If x$ = "" Then redo = 3: b$ = Chr$(8): Exit While
                End Select
            End If
            If Len(_Clipboard$) Then GoSub clip: dir2$ = x$: Exit Do
        Loop
        _Clipboard$ = Chr$(0)
        If Right$(dir2$, 1) <> "\" Then dir2$ = dir2$ + "\"
        Print dir2$
        If _DirExists(dir2$) Then Exit While Else Print "Directory not found: "; dir2$; " Any key to redo...";: Sleep
    Wend
    If b$ = Chr$(8) Then Exit Do
    redo4:
    While -1
        Locate 13, 1: View Print CsrLin To _Height - 1: Cls 2: View Print
        Locate 13, 1: Print "Paste second file name: ": Locate 15, 1
        If Len(filehold$) Then
            file2$ = filehold$: filehold$ = ""
        Else
            Do
                _Limit 60
                b$ = InKey$
                If Len(b$) Then
                    Select Case b$
                        Case Chr$(27): System
                        Case Chr$(8): redo = 3: Exit While
                        Case Chr$(9): file2$ = file1$: Exit Do
                        Case Chr$(32): GoSub manual: If x$ = "" Then redo = 4: b$ = Chr$(8): Exit While
                    End Select
                End If
                If Len(_Clipboard$) Then GoSub clip: file2$ = x$: Exit Do
            Loop
        End If
        _Clipboard$ = Chr$(0)
        If InStr(file2$, ".") = 0 Then file2$ = file2$ + ".bas"
        Print file2$
        If _FileExists(dir2$ + file2$) Then Exit Do Else Print "File not found: "; dir2$ + file2$; " Any key to redo...";: Sleep
    Wend
    If b$ = Chr$(8) Then Exit Do
Loop
If b$ = Chr$(8) Then
    On redo GOTO redo1, redo2, redo3, redo4
End If
If dir1$ + file1$ = dir2$ + file2$ Then
    Beep
    Print "Error: Both directory and file names are the same. Cannot compare the same file. Any key to redo": Sleep
    If InKey$ = Chr$(27) Then System Else Run
End If
Print: Print "Ready. Press any key to begin comparison...": Sleep
Width 150, 41
_Font 16
_ScreenMove 20, 0

start:
Cls
Locate 1, 1: Color 15, 1: Print Space$(_Width);
Locate 21, 1: Print Space$(_Width);
Locate 1, 2: Color 15, 1: Print dir1$;: Color 14, 1: Print file1$;
Locate 21, 2: Color 15, 1: Print dir2$;: Color 14, 1: Print file2$;
Color 7, 0
ReDim c1$(10000), c2$(10000): c1 = 0: p1 = 0: c2 = 0: p2 = 0
' Quick compare...
If Not _FileExists(dir1$ + file1$) Then
    Print: Print "Cannot find file: " + dir1$ + file1$ + ". Any key to retry.": _Delay 1: Sleep
End If
If Not _FileExists(dir2$ + file2$) Then
    Print: Print "Cannot find file: " + dir2$ + file2$ + ". Any key to retry.": _Delay 1: Sleep
End If
Open dir1$ + file1$ For Binary As #1
Open dir2$ + file2$ For Binary As #2
x1$ = Space$(LOF(1))
x2$ = Space$(LOF(2))
Get #1, , x1$
Get #2, , x2$
Close #1, 2
If x1$ = x2$ Then
    Locate 41, 2: Print "Both files are identical. Press Enter to rerun or Esc to quit...";
    Do
        _Limit 30
        b$ = InKey$
        If Len(b$) Then
            Select Case b$
                Case Chr$(27)
                    System
                Case Chr$(13): Cls: _Delay 1: Run
            End Select
        End If
    Loop
End If
Open dir1$ + file1$ For Input As #1
Open dir2$ + file2$ For Input As #2
Do Until EOF(1)
    Line Input #1, a$
    a$ = RTrim$(LTrim$(a$))
    If Mid$(a$, 1, 1) > " " Then
        c1 = c1 + 1
        c1$(c1) = a$
    End If
Loop
Close #1
Do Until EOF(2)
    Line Input #2, a$
    a$ = RTrim$(LTrim$(a$))
    If Mid$(a$, 1, 1) > " " Then
        c2 = c2 + 1
        c2$(c2) = a$
    End If
Loop
Close #2
Locate 2, 1
onscr1$ = c1$(1)
onscr2$ = c1$(1)
yy1 = v1top: yy2 = v2top
p1 = 0: p2 = 0: auto = -1
Do
    _Limit 300 ' Controls speed of screen display.
    If auto = -1 And b$ <> Chr$(13) Then
        If onscr1$ <> onscr2$ Then
            Locate 41, 2, 1, 7, 30
            Color 14, 0
            Print "No Match! ";
            auto = 0
            Color 7, 0
        End If
    End If
    If auto Then
        p$ = InKey$
        Select Case p$
            Case Chr$(27): System
            Case Chr$(32)
                y = CsrLin: x = Pos(0): Locate 41, 2: Color 14, 0: Print " Paused...";: Color 7, 0
                Do: _Limit 30: p$ = InKey$: Loop Until p$ = Chr$(32)
                Locate 41, 2: Print "          ";
                Locate y, x
        End Select
        If auto < 0 Then b$ = "1": auto = 1 Else b$ = "2": auto = -1
    Else
        b$ = InKey$
    End If
    If Len(b$) Then
        Select Case b$
            Case Chr$(27): System
            Case Chr$(0) + Chr$(59): _Clipboard$ = c1$(p1)
            Case Chr$(0) + Chr$(60): _Clipboard$ = c2$(p2)
            Case Chr$(13), Chr$(0) + "P": b$ = Chr$(13): auto = -1: Locate 41, 2, 1, 7, 0: Print "          ";
            Case "1", "2"
                Locate 41, 2, 1, 7, 0: Print "          ";
                If b$ = "1" And Len(c1$(p1 + 1)) Then
                    View Print v1top To v1btm
                    p1 = p1 + 1
                    Locate yy1, 1: Print p1; c1$(p1): yy1 = CsrLin
                    onscr1$ = c1$(p1)
                End If
                If b$ = "2" And Len(c2$(p2 + 1)) Then
                    View Print v2top To v2btm
                    p2 = p2 + 1
                    Locate yy2, 1: Print p2; c2$(p2): yy2 = CsrLin
                    onscr2$ = c2$(p2)
                End If
        End Select
    End If
    If p1 = c1 And p2 = c2 Then Exit Do
Loop
Close
Locate _Height, 2: Color 14, 0: Print "Finished. [R]un [V]iew Again [Q]uit: ";: Locate , , 1, 7, 30
Color 7, 0
Do
    _Limit 30
    b$ = InKey$
    If Len(b$) Then
        If UCase$(b$) = "Q" Or b$ = Chr$(27) Then System
        If UCase$(b$) = "R" Then _Clipboard$ = hold$: Cls: Run
        If UCase$(b$) = "V" Then View Print: GoTo start
    End If
Loop
Locate _Height, 1: Print Space$(_Width - 1);
Locate _Height, 1: Print " Restore prior clipboard contents to clipboard? Y/N";
Do
    b$ = InKey$
    _Limit 30
    If Len(b$) Then
        If b$ = Chr$(27) Then System
        If UCase$(b$) = "Y" Then Print "Y";: _Delay 2: _Clipboard$ = hold$: Exit Do
        If UCase$(b$) = "N" Then Exit Do
    End If
Loop
System

clip:
Sound 1000, .3
x$ = _Trim$(_Clipboard$)
If InStr(x$, Chr$(13)) Then x$ = Mid$(x$, 1, InStr(x$, Chr$(13)) - 1)
If InStr(x$, ".") And InStr(x$, "\") <> 0 Then
    If dir1$ = "" Then
        filehold$ = Mid$(x$, _InStrRev(x$, "\") + 1)
        x$ = Mid$(x$, 1, _InStrRev(x$, "\"))
    ElseIf dir2$ = "" Then
        filehold$ = Mid$(x$, _InStrRev(x$, "\") + 1)
        x$ = Mid$(x$, 1, _InStrRev(x$, "\"))
    End If
End If
Return

manual:
y = CsrLin: x = 12
Line Input "Type name: ", x$: Locate y, x
If Len(x$) Then _Clipboard$ = x$ Else _Clipboard$ = Chr$(0)
Return

Oh, and if you're a REAL lazy SOB like me, you'll want to compile this as an exe and make a desktop shortcut.

Code: (Select All)
count = _CommandCount
For i = 1 To count
    cmd$ = Command$(i)
    Print cmd$
    _Clipboard$ = Command$(i) ' Dragged files from File Explorer provide the path and file name, here.
Next
System

1) Compile as an exe and make a desktop shortcut (Send to Desktop). Don't run this program. Run the FIRST program and instead of 4 times copying to the clipboard, you can just DRAG each of the two files you want compared from File Explorer into that desktop shortcut, one at a time. So just two input operations. The program does the rest, and shows you the results on the screen.

I would recommend trying it out on a couple of files you already have backed up, just for your own piece of mind.

THE ROUTINE DOES USE ONE INSTANCE OF OPEN FOR BINARY. Note it does not use PUT, so it won't change your files. Also, it checks for file and directory existence, so it won't make any 0 byte files, either. Honestly, in 25 years of online forums, I've only encountered one 'person' who would intentionally try to mess others up. Still, I think it is always wise to check code, before you run it, for keywords like KILL, BINARY, OUTPUT, etc. to see how they are used.

   

Let me know if you have any questions. The keys are listed at the top of the code.

Pete