Sorting REG & INI files For Easy Comparison

Here is a great free sorting utility by Jacques Bensimon. Enjoy!


A common task when troubleshooting Windows images and user profiles is comparing two exported Registry keys or entire hives, and sometimes two INI files, to discover where differences lie.  This approach is sometimes a last resort when trying to answer questions such as “Why does app XYZ work on computer A but not on computer B?”, or “Why does app XYZ work for user A but not for user B?”.  It can also be used, if you don’t have access to the previously recommended Total Uninstall, to discover where and how a machine’s or user’s Registry contents change when an application or system component is installed or reconfigured, by comparing REG files exported before and after the operation of interest (using, for example, Microsoft’s WinDiff or the freeware WinMerge file comparison tools).

The problem however is that, unlike the strictly alphabetical sorting of Registry key entries that Regedit’s GUI presents, the entries under any given key in an exported REG file are listed in the order they were first written to the Registry at that key, which can make a mess of any attempt to compare two theoretically similar REG files as the comparison tool’s display becomes polluted with indicators of otherwise identical lines found at different locations in the two files.  A similar issue can arise when comparing two large INI files, including in this case the possibility that the keys (”sections”) themselves might also be listed in different orders in the two files.  [While exported REG files are guaranteed to list keys in alphabetical order, the same cannot be said of REG files created by aggregating together multiple exports, so out-of-order keys are also a possibility with REG files.]

This is where the RegSort utility presented in this post comes in:  when applied to one or more REG or INI files, it’ll create sorted versions of the input files (which are not themselves modified) in a canonical format guaranteed to list keys in alphabetical order, and the values under each key also in alphabetical order.  Any differences uncovered by a comparison tool applied to the sorted versions of two REG or INI files will therefore be “true” differences rather than uninteresting differences in line positions.  RegSort’s syntax is simple enough, as shown in the following dialog displayed by running the utility without a parameter (or with the parameter /?):

So, for example, if folder C:\Temp contains REG files A.reg and B.reg, running RegSort C:\Temp\*.reg will create A_Sorted.reg and B_Sorted.reg in the same folder, overwriting any existing instances of those files and ignoring any other files matching *_Sorted.reg.  When processing large files, for example, exports of entire HKCU or HKLM\Software keys (which can take a significant amount of time), a dialog showing the file and key currently being processed (and an indication of progress through the file) will be displayed:

Notes:

  • To stress a requirement listed in the syntax dialog above, the input file(s) MUST be in ANSI format (the traditional “REGEDIT4” format in the case of REG files, not the newer Unicode “Windows Registry Editor Version 5.00” format), so when exporting from Regedit, use the “Win9x/NT4 Registration Files” option rather than the default “Registration Files” option.  If using Regedit.exe to export from the command line, use /A rather than /E to export in ANSI format.  [The Export command in current versions of the Reg.exe utility unfortunately no longer support the equivalent /NT4 switch].
  • Long binary Registry entries that Regedit exports as multiple lines (with a trailing backslash character to indicate continued lines) are collapsed as single-line entries in the sorted output files. This remains valid REG file syntax regardless of the resulting line lengths (as Regedit’s multi-line format is purely aesthetic and not a requirement when importing a REG file).
  • At the risk of stating the obvious, before comparing REG files exported from two different root keys (for example from HKU\{User-SID-1} and HKU\{User-SID-2} when comparing two user profiles, or from HKLM\SYSTEM\ControlSet001 and HKLM\SYSTEM\ControlSet002 when comparing current and prior system devices, services, etc.), a text editor’s search & replace capability should be used on one or both files to give them a common root key.  For instance, in the first example above, instances of “[HKEY_USERS\{User-SID}” in both exported REG files could be replaced with say “[HKEY_CURRENT_USER”.  This substitution can be performed either on the original REG files before using RegSort or on the resulting sorted files before comparison.
  • As can be seen in the sample progress dialog above while a file named “HKLM_Small.rek” is being processed, RegSort doesn’t require the input filename(s) to have the .reg or .ini extension.  Each sorted output file will be given the same extension as its parent unsorted version.

RegSort has been a very useful tool for me over the years, and I was surprised to realize recently that I’d never made it available more widely than to just my colleagues.  Several years late, but omission hopefully corrected. You can download it here below:

JB

Follow Jacques Bensimon on Twitter @JacqBens for more great Windows insights and tricks.

TAGS