How to Trigger DiffMerge from XYplorer

As part of my day job I work with lots of files and one of the common activities I need to carry out is to compare the contents of those files to find omissions and inconsistencies. Fortunately most of the files I need to compare are pure text based files (e.g. XML, XSL, LOG, TXT, INI) and there are plenty of tools available to help work with those files. To work with files in general on Windows I make use of the XYplorer file manager and fortunately for me it is pretty easy to integrate my comparison tool of choice into it.

DiffMerge

DiffMerge by Sourcegear is my comparison tool of choice. It hasn’t been updated in a little while but it is a stable and capable tool. It works well with the typical comparisons I need to carry out and whilst on the more complex data structures it is far from perfect I can’t imagine there is anything more suited to the task except perhaps intelligence based solutions.

DiffMerge comes with some great Explorer integrations that are also available within XYplorer. If you select two files in a window you can get a context menu option (when you right click) to compare the files (A to B or B to A). If you select individual files then you can get an option to remember a file and compare the currently selected file with a remembered file.

Whilst you can also open files from within DiffMerge itself I’m always in my file manager when working on comparing files and so it’s these selection features that really make the difference.

XYplorer Integration

As I mentioned above the Explorer context menu options are also available in XYplorer - though the same context menu options; so what additional integration is beneficial? The answer to this comes down to a feature in XYplorer’s interface.

XYplorer has an option to display a dual pane interface and this is a configuration option I use exclusively when comparing folders of files. Originally for this set-up I had to use the remember file based comparison to select and remember a file in one pane and then do the same in the other pane.

The change came when I decided to take a look at scripting some of the interactions across panes to see if I could cut out this manual step using the command line arguments for DiffMerge (a technique we used with an in-house configuration analysis tool developed by one of my colleagues).

I started by simply looking at what I could do to compare the selected file in the left pane to the selected file in the right pane. Here’s the script I created.

    "Left vs. Right";
      $file1 = get("SelectedItemsPathNames", , 1);
      $file2 = get("SelectedItemsPathNames", , 2);
      run """C:\Program Files\SourceGear\Common\DiffMerge\sgdm.exe"" ""$file1"" ""$file2""";

The first line is simply the name of the script. The second and third lines are simply getting the paths of the selected file in each pane. Technically they are getting a list of paths (separated by new lines) of all selected files in each pane but since I want to only compare one at a time and the default use is to just pick the first selection I’ve not seen a need just yet to add in any further defensive scripting around this.

The final line is the one that calls DiffMerge. It’s pretty easy to read the intent and it’s only the double quotes that make it a little difficult to read. As long as you take the first double quote and the last double quote on the line as the string delimiters you should then be able to see every other double quote occurrence is part of a pair of double quotes. These are just ‘escaped’ double quotes as we want to ensure our path names are quoted strings in case they contain spaces which would mess up the parameters being passed into DiffMerge.

I added this script to a custom button in the XYplorer interface and now I can simply click on the button to carry out this comparison.

Whilst it is by far the most common comparison operation I do sometimes I want to do the opposite and compare the file in the right most pane to the one in the left most. Whilst I can swap it over once loaded into DiffMerge it’s a little quicker to load the files in for comparison in the right order rather than re-load and re-analyse after the fact. It was a trivial matter to achieve this just by swapping over the parameter order in a clone of the previous script.

    "Right vs. Left";
      $file1 = get("SelectedItemsPathNames", , 1);
      $file2 = get("SelectedItemsPathNames", , 2);
      run """C:\Program Files\SourceGear\Common\DiffMerge\sgdm.exe"" ""$file2"" ""$file1""";

Custom buttons in XYplorer can be right clicked as well as left clicked. Different scripts can be added to both types of click. I therefore left the left click to just be the left vs. right script but added both scripts to the right click. When I right click on this button I then got an option to choose which script to run as well as the button customisation options below that on the context menu.

This wasn’t the end of the additions though. I soon realised that the sorts of comparisons I was doing where I was using the dual pane view would be such that I’d be comparing files with the same name. As a result if I had both panes in the right locations I really only needed to select one file in a pane to carry out the comparison. Thus saving a bit of scrolling and selecting which over time adds up when you are comparing hundreds of files … not the most thrilling part of my work I have to admit.

The script I put together for this foregoes selecting a left and right and instead works based on the active pane. It just seemed a more natural way to do it to me if I was focussing in on one pane and using the other more as an area input for the comparison rather than a specific file input.

    "Active File to Corresponding";
      if (get("Pane") == 1)
      {
        $strAltPanePath = get("Path", 2);
      }
      else
      {
        $strAltPanePath = get("Path", 1);
      }

      $file1 = get("SelectedItemsPathNames");
      $file2 = $strAltPanePath."\".get("SelectedItemsNames");
      run """C:\Program Files\SourceGear\Common\DiffMerge\sgdm.exe"" ""$file1"" ""$file2""";

The structure of the script is quite similar to the previous ones as you might expect. We’re looking to get two file paths to pass into DiffMerge on the command line. The difference is of course how we get those paths.

The script starts out by determining which pane is active and then storing the path for the other pane into the variable $strAltPanePath.

The script then gets the path of the currently selected file(s) in the active pane and this is set to be the first file path.

The script then gets the name of that selected file and prefixes it with the path of the inactive pane to create the second file path.

The final line then runs the familiar command to trigger DiffMerge.

I again created the inverse of this just so it was available for any occasion where I needed to reverse the comparison order.

    "Corresponding to Active File";
      if (get("Pane") == 1)
      {
        $strAltPanePath = get("Path", 2);
      }
      else
      {
        $strAltPanePath = get("Path", 1);
      }

      $file1 = get("SelectedItemsPathNames");
      $file2 = $strAltPanePath."\".get("SelectedItemsNames");
      run """C:\Program Files\SourceGear\Common\DiffMerge\sgdm.exe"" ""$file2"" ""$file1""";

These two scripts were added to the right click option of the custom button and so I now have my run of the mill left vs. right option that triggers immediately on left click and an option of four other comparisons if I right click on the button.

DiffMerge and XYplorer are great tools in their own right but with a little bit of scripting you can take their integration up a notch and save yourself some time.

Author: Stephen Millard
Tags: | xyplorer |

Buy me a coffeeBuy me a coffee



Related posts that you may also like to read