Notice: This forum has been recovered from an old backup, so some content, links, and dates may be outdated. The forum is currently read-only while we restore sign-in and registration functionality. Details

If you find this forum valuable and would like to help keep it online, donations to help cover hosting and domain costs are greatly appreciated, but never expected. You can support the forum through Buy Me a Coffee or Ko-fi. Thank you for helping preserve the EventGhost community.

Comparing 2 files

If you have a question or need help, this is the place to be.
Post Reply
eventspook
Posts: 23
Joined: Tue Mar 05, 2013 2:50 pm

Comparing 2 files

Post by eventspook »

What's the easiest way of comparing 2 files with EventGhost?

Each file will have a single line, and if the line is different, I want it to display the line, then copy it to the other file for future comparisons.

I'm guessing I'll have to resort to Python code, or am I missing a more obvious solution?
kkl
Experienced User
Posts: 317
Joined: Wed May 04, 2011 9:32 pm

Re: Comparing 2 files

Post by kkl »

You might want to take a look at the File Operations plug-in. It'll at least give you a method for reading the files.
eventspook
Posts: 23
Joined: Tue Mar 05, 2013 2:50 pm

Re: Comparing 2 files

Post by eventspook »

Yep, that's what I am using to read the content, but not sure how to store the content in a 2nd file, and then compare, without code.
User avatar
jinxdone
Plugin Developer
Posts: 443
Joined: Tue Jan 02, 2007 4:08 pm

Re: Comparing 2 files

Post by jinxdone »

Python only solution, Copy & paste, edit the python script and change file locations

Code: Select all

<?xml version="1.0" encoding="UTF-8" ?>
<EventGhost Version="1610">
    <Macro Name="Compare Files" Expanded="True">
        <Action Name="Compare files, copy file if changed, show contents">
            EventGhost.PythonScript(u"import shutil\nfile1 = u'q:\\\\1.txt'\nfile2 = u'q:\\\\2.txt'\nline1 = eg.plugins.FileOperations.Read(0, file1, 0, 0, 'cp1252', 1, False, 1)\nline2 = eg.plugins.FileOperations.Read(0, file2, 0, 0, 'cp1252', 1, False, 1)\nif line1 != line2 :\n    #Files were NOT the same\n    shutil.copy(file1, file2)\n    eg.plugins.EventGhost.ShowOSD(line1)\n\n")
        </Action>
    </Macro>
</EventGhost>
---

Ok this is a little bit offtopic because it's not a 100% EG/python solution. But you could utilize the helper apps that already exist in windows. With windows' fc.exe it you can do either in text or binary comparison (I think fc.exe has been there at least since MS-DOS 3.3). For example.. (edit the python script/actions and change file names/locations inside)

As items in the config tree:

Code: Select all

<?xml version="1.0" encoding="UTF-8" ?>
<EventGhost Version="1610">
    <Folder Name="File Compare" Expanded="True">
        <Macro Name="Compare Files" Expanded="True">
            <Event Name="MYEVENT" />
            <Action>
                System.Execute(u'fc.exe', u'q:\\1.txt q:\\2.txt', 3, True, 2, u'', False)
            </Action>
            <Action>
                EventGhost.NewJumpIf(XmlIdLink(1501), 0, False)
            </Action>
        </Macro>
        <Macro Name="Copy file, Show file contents" id="1501" Expanded="True">
            <Action Name="Copy file">
                EventGhost.PythonScript(u"import shutil\nshutil.copy(u'q:\\\\1.txt', u'q:\\\\2.txt')")
            </Action>
            <Action>
                FileOperations.Read(0, u'q:\\1.txt', 0, 0, 'cp1252', 1, False, 1)
            </Action>
            <Action>
                EventGhost.ShowOSD(u'{eg.result}', u'0;-30;0;0;0;700;0;0;0;1;0;0;2;32;Arial', (255, 255, 255), (0, 0, 0), 0, (0, 0), 0, 3.0, False)
            </Action>
        </Macro>
    </Folder>
</EventGhost>
Or as a single python script:

Code: Select all

<?xml version="1.0" encoding="UTF-8" ?>
<EventGhost Version="1610">
    <Macro Name="Compare Files" Expanded="True">
        <Action Name="Compare files, copy file if changed, show contents, using fc.exe">
            EventGhost.PythonScript(u"import shutil\nShowOSD = eg.plugins.EventGhost.actions['ShowOSD']()\nfile1 = u'q:\\\\1.txt'\nfile2 = u'q:\\\\2.txt'\nif eg.plugins.System.Execute(u'fc.exe', file1 + u' ' + file2, 3, True, 2, u'', False):\n    #Files were NOT the same\n    shutil.copy(file1, file2)\n    ShowOSD(eg.plugins.FileOperations.Read(0, file2, 0, 0, 'cp1252', 1, False, 1))\n\n")
        </Action>
    </Macro>
</EventGhost>>
There is also an advanced copy tool for windows called robocopy.exe which can often be useful when doing things like this. It can be used to mirror folders or copy files only when they are newer/different size. It should be included in Win7, for older versions of windows you can get it from the "Windows 2000 Resource kit" or similar from Microsoft.


But you are right, I suppose doing it only via the config tree would need adding a "Compare" action for the FileOperations plugin. I have usually just resorted to writing little helper scripts like this when I need to..

Hopefully this was helpful..
eventspook
Posts: 23
Joined: Tue Mar 05, 2013 2:50 pm

Re: Comparing 2 files

Post by eventspook »

More than helpful, since I am new to Python (and the way it is integrated into EventGhost), these examples are extremely helpful. The fact I can use dos commands makes this thread extremely valuable to me, as it opens a whole new world of options. Thanks!
Post Reply