For some unknown reason the solution @guy038 posted above doesn’t work anymore or at least not always. Thus I want to share an alternative solution. The basics for it have been found by @Ekopalypse in this thread.
To accomplish the comparison task in a user-friendly way we need the following:
The Compare plugin (obviously).
The NppExec plugin.
An NppExec script to start comparison.
A Batch script as the controller.
A VBScript as launcher for the Batch script to avoid flickering console windows.
An Explorer context menu entry.
The Compare Plugin
The Compare plugin can be installed using PluginManager (when using Notepad++ v7.5.9 or below) or PluginsAdmin (when using Notepad++ v7.6 or above). It is also possible to download it from this site (click on releases in the menu bar) and install it manually.
The NppExec Plugin
The NppExec plugin can be installed using PluginManager (when using Notepad++ v7.5.9 or below) or PluginsAdmin (when using Notepad++ v7.6 or above). It is also possible to donwload it from this site (click on releases in the menu bar) and install it manually.
The NppExec Script
After installing the NppExec plugin navigate in Notepad++ to (menu) Plugins -> NppExec -> Execute.... Copy the following code and paste it into the edit area of the popped up window.
if $(SYS.NppCommand) == CompareFiles then
npp_menucommand Plugins/Compare/Compare
endif
Please note: The npp_menucommand command requieres the path to the menu item it should simulate a click on to be localized. That means you have to provide the name of the Plugins menu in the locale your Notepad++ UI is set to.
The code checks if there is an environment variable NppCommand with the value CompareFiles. If this is the case it triggers the Compare plugin to start file comparison by simulating a mouse click on the according menu entry.
Now click on button Save... and enter CompareFilesOnStartup into the combo box. This is the name the script can be called with. Then click the Save button and close the dialog by clicking the OK button.
Now navigate to (menu) Plugins -> NppExec -> Advanced Options... and select in the combo box under Execute this script when Notepad++ starts the CompareFilesOnStartup script you created above. Close the dialog by clicking the OK button.
The Controlling Batch Script
Open a new file in Notepad++ and paste the code below into it. Store it as NppCompareFiles.cmd.
@echo off & setlocal
set "File1Buffer=%TEMP%\NppCompare_File1.txt"
if "%~2" neq "" (
set "File1=%~1"
set "File2=%~2"
) else if not exist "%File1Buffer%" (
(set /p "=%~1" < NUL & echo.) > "%File1Buffer%"
exit /b 0
) else (
set /p "File1=" < "%File1Buffer%"
set "File2=%~1"
)
set "NppCommand=CompareFiles"
start "" "%ProgramFiles(x86)%\Notepad++\notepad++.exe" "%File1%" "%File2%"
del "%File1Buffer%" 1>NUL 2>NUL
The script can be called once, providing two file paths as parameters. In lines 6 and 7 they are copied to two internal variables.
The script can also be called twice, providing only one file path each. In this case it checks if a buffer file for storing the first file’s path already exists. If not it copies the provided file path to that buffer file and exits (lines 9 to 11).
To avoid problems with file paths containing special characters which would be interpreted as control characters by the Batch script interpreter, I use the set /p command to write the file path to the buffer file. This way I can use double quotes, which prevent the evaluation of control characters by the Batch script interpreter, without writing these quotes to the buffer file.
If the buffer file already exists, the contained file path is read from it and stored into an internal variable to be used as the first file for comparison. The file path of the second file is taken from the provided parameter (lines 13 to 15).
As a last step an environment variable named NppCommand is created and set to the value CompareFiles (this variable is queried in the NppExec script above). After that Notepad++ is executed with the two file paths retrieved before as arguments (lines 18 to 19). By using the start command it is possible to avoid the script getting paused until Notepad++ is closed. Instead execution continues immediatly.
Finally the buffer file for the first file’s path is deleted (line 21).
The VBScript Launcher Script
It would be possible to use the Batch script created above directly. But that would cause console windows popping up and disappearing immediatly, an ugly flickering effect.
For that reason we need the following launcher script. Open a new file in Notepad++, paste the code below into it and save it as NppCompareFilesLauncher.vbs.
If WScript.Arguments.Count = 0 Then WScript.Quit
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objWshShell = CreateObject("WScript.Shell")
strScriptPath = objFSO.GetParentFolderName(WScript.ScriptFullName)
strScriptFullPath = objFSO.BuildPath(strScriptPath, "NppCompareFiles.cmd")
strArgs = ""
For intIdx = 0 To WScript.Arguments.Count - 1
strArgs = strArgs & """" & WScript.Arguments(intIdx) & """ "
Next
objWshShell.Run """" & strScriptFullPath & """ " & strArgs, 0, False
The script collects all the provided command line arguments one by one and generates a new command line consisting of the full path to the NppCompareFiles.cmd script and all the original command line arguments. Then it executes the Batch script in a hidden window, which avoids flickering console windows.
The Explorer Context Menu Entry
At first move both the two files NppCompareFiles.cmd and NppCompareFilesLauncher.vbs to a folder of your choice. For this How-To I will use C:\Program Files (x86)\Notepad++\plugins\ComparePlugin\ComparePlugin.
Open a new file in Notepad++, paste the code below into it and save it as NppCompareFiles.reg. In line 7 you have to provide the folder where you stored the two script files in the last step.
Please note: Strings in *.reg files have to follow the escaping rules of strings in the C programming language. That means that e.g. " and \ have to be escaped with the \ character, which causes the double-backslashes in the file’s content below.
Windows Registry Editor Version 5.00
[HKEY_CLASSES_ROOT\*\shell\CompareWithNpp]
@="Compare with Notepad++"
[HKEY_CLASSES_ROOT\*\shell\CompareWithNpp\command]
@="wscript.exe /nologo \"C:\\Program Files (x86)\\Notepad++\\plugins\\ComparePlugin\\ComparePlugin\\NppCompareFilesLauncher.vbs\" \"%1\""
Now double-click the resulting *.reg file and confirm the UAC dialog and all other dialogs. This will install an entry named Compare with Notepad++ in the Explorer context menu of all files. After that you can delete the *.reg file, it is no longer needed.
First Try
Now close Notepad++. Open an Explorer window, right-click one file, select Compare with Notepad++ from the context menu, right-click another file and do the same. Notepad++ should open and the files should be compared by the Compare plugin.
Close Notepad++ again. Select two files at once, right-click one of them and select Compare with Notepad++ from the context menu. Notepad++ should open and the files should be compared by the Compare plugin.