Wednesday, June 28, 2017

File MD5 Checksum - How to tell if a file has changed

File MD5 Checksum - How to tell if a file has changed
File MD5 Report - Batch MD5 Reporting

A file's MD5 Checksum can tell you when a file has been modified from the author's original version.  For example, when downloading a program from the Internet, developers often will give you the file's MD5 check-sum -- your version's MD5 could be compared with the author's to see if there was a change.  

Seeing a file's MD5 checksum, sometimes called a 'hash,' requires software.  I have written a handy utility, which you can download. 

This utility can report the MD5-hash for a single file.  But more interestingly, it can keep track all MD5's in a directory -- keeping a mini-inventory.  With this, you can tell when a file has been added, changed or deleted! -- regardless of the filename or date-time-stamp.


Problem at the Office:

At the office, I had a directory of photographs that are weekly refreshed by another system.  Each week the entire inventory was replaced.  Original filenames and sizes remained the same, but because the files were re-downloaded each week, the date-time-stamp changed.  I needed a way to tell which files were different. 

This Keyliner utility resolved these problems.  It is able to tell if the same-named, same-sized photo contained different contents and it can tell who is new and who has been deleted. 


Download Utility

Download this Keyliner utility, from Keyliner's GDrive:

Link: FileChangeMD5.EXE

This utility is free for all personal and commercial use.
No registration.  No logging in.  No email.  No nag screens. 

Ironically, it has this MD5: 54-d5-e7-f6-59-7a-96-6b-6c-0e-f8-90-ac-95-b9-5d


Click the download link and save to a known directory -- but do not run from the browser (you won't be able to see the results).  The program is best run from a command-line prompt.  (Start, Run, CMD.  Then change to the download directory and type 'FileChangeMD5.exe'). 

Since the executable is not signed (too expensive), Windows will warn you of an un-trusted program downloaded from the internet.  If you trust my program, accept with a "Run anyway".  You will only be prompted for this once.





Features:
  • Show a specific file's MD5 check sum, using this command-line

    FileChangeMD5.exe file=filename.ext
     
  • Optionally, using a configuration file, report on all file-changes within a directory, showing
    -Changed Files
    -New Files
    -Deleted Files

    For example:
    FileChangeMD5.exe c:\temp\filechangeINI.MD5
     
  • Keeps track of changed files by using a text-based database - no database to install or maintain
  • Output is an ASCII tab-delimited file (default name: "Inventory.MD5"), suitable for Excel or other programming
     
  • Runs in Batch or interactive command-prompt mode
  • Runs unbelievably fast; directory is parsed in sub-seconds
     
  • Can report, grouping by changed-status, or by filename
  • Controlled with text-based INI files; multiple INI files, multiple directories, etc.
     
  • Free, no charge, no registration, no nags
  • Simple EXE - No installation required
  • No registry changes; no databases
     

Example Output:

This shows the inventory of all files in a directory (C:\Temp).  Notice the Status column:  "nochange", "changed", "new", and "deleted".  Each file's checksum/MD5 is listed:

Click for larger view

This file can be consumed by other software, parsing for the Status Column.  See also, the optional InventoryReport, below, for a similar inventory file.


Optional Inventory Reporting:

If the config file's "InventoryReport" field is populated with a file name, a secondary inventory report is generated.  The is a simplified report, showing only the type of change and the file name.






Individual File MD5 Checksum

The utility, without any configuration or setup, can report a single file's MD5 checksum, showing the results to the console. 

This example shows the MD5 hash for the file named "C:\Temp\file-01.txt"

Click for larger view

To view a single file, run the program from a Command prompt.
Use this command: 

filechangemd5.exe  file="C:\path\somefile.ext"

where the keyword "file=" is required. 
use quotes around the path and filename.

If you download this utility, use the downloaded program to check its own MD5!  Use this DOS command:

FileChangeMD5.exe fileChangeMD5.exe

Compare with my published hash near the top of this article.



Installation and Configuration:

There is no real installation to this program.  Just copy the EXE.  But if you want to inventory a directory, it requires minor notepad editing changes to a configuration file.

1.  Download FileChangeMD5.exe from Keyliner's GDrive

2.  Place executable file in any directory
     I recommend a data-folder, such as C:\Data\Programs\FileChangeMD5 or any directory.

Special Note:
If saved in "C:\Program Files"  or "C:\Program Files (x86)",
you would have to give your user-account unusual rights to update files in this protected area.
Putting the executable program in Data folder resolves this problem.

* See Advanced Installation, below, for more information on the Program Files directory.

3.  Build a sample configuration file.

     From a Command Prompt, or with Windows Explorer,
     Double-click (launch) the program for an initialization-run.

     This generates a default INI file, named "FileChangeINI.MD5
     The file arrives in the same directory as the executable. 
     (You must have rights to write the file in this directory)

Click for larger view


Notes:  
The configuration file "FileChangeINI.MD5" is an INI file, but does not use an INI file-extension.  Reason:  You do not want this file to show up in the inventory database.  Files with an ".MD5" extension are ignored by the utility.  With this, the files "Inventory.MD5" and "FileChangeINI.MD5", can live in the same directory as the files you are inventorying.

If the INI file is not generated, you do not have enough rights in that directory.  Run from a DOS prompt to see the error message.  Likely issue:  See below, Advanced Installation 


4.  Using Notepad, edit the file and change the FileDirectory path from
     C:\Temp  to a directory of your choosing.

     Close and save the configuration file.



TESTING:

Create a test directory (C:\TEMP) and populate the folder with expendable files.

A.  From a Command Prompt (DOS prompt), re-launch the program

     C:
  CD\Data\Programs\FileChangeMD5
  FileChangeMD5.EXE


     This runs the program and builds an inventory of the (C:\Temp) directory.
     The exe must be in a folder where your user-account has rights to update files.
     Often, Program Files folders are restricted.  See below for more advanced options.


Notes: 
When launched, the EXE looks in the current directory for a config file, unless told otherwise.

Notes:
Optionally move the config/INI file to any directory, any name.
Launch with this command line: 
FileChangeMD5.exe C:\mypath\myconfigINI.MD5


B.  Start, run "Notepad.exe"

      Tunnel to the directory where the Program/INI was installed.
      Select file-open, and open this file: "Inventory.MD5"

  * "Inventory.MD5" is the inventory of changed files and the first-time run
     is the base-run.  

     The first-time run, all files show as "New". 



     Close Notepad.

C.  Run the executable a second time (with no file changes)

      Using Notepad, re-open "Inventory.MD5"
      All files now show as "unchanged"  e.g. no changes to the base-inventory since the last run.



D.  Edit any test file in (C:\TEMP), making any innocuous change. Save the change.

     Consider deleting a file
     Consider adding a new, unexpected file

E.  After changing files, run the executable a third time.

     Using notepad, open file "InventoryReport.MD5"
     Note file changes in the status column:

   

To Force a re-inventory of all files in (C:\TEMP)

1.  Erase file "Inventory.MD5" and re-run the program.
     All files are re-detected as New.

To Force a single-file to be detected as "changed"
(for example, you need a downstream routine to re-process it), do the following:

1.  Using Notepad, open the current inventory file:  "Inventory.MD5"
 
2.  Edit the file's MD5 hash, changing to any random number.
     You only need to change one digit to invalidate the checksum

Click for larger view
3.  Re-run the program.  The file will show as "changed"


Advanced Installation Notes:

Microsoft has restricted "C:\Program Files (x86)" and "C:\Program Files" from unsigned, and un-installed programs.  If you are like me, you really want your utility programs in these folders, where they are protected.  The trouble is, the program wants to write a default configuration INI file in this same area.  This is resolved with these steps:

Build a default FileChangeINI.MD5 control file:

A.  Temporarily copy "FileChangeMD5.exe" to a data folder (such as MyDownloads, C:\data, etc.)

B.  Launch the program one time (double-clicking).  It will have enough rights to write its default configuration file, "FileChangeINI.MD5" -- or copy the file from below and avoid the temporary steps.

C.  Move the executable, "FileChangeMD5.exe", back to your favorite ("C:\Program Files\Util", etc.) folder.  The temporary copy in (MyDownloads, c:\data, etc.) can be deleted.

D.  From the temporary copy's directory, move the newly-constructed INI file, "FileChangeINI.MD5" to any directory of your choosing.

      For example,  C:\Data\Jobs\FileChangeINI.MD5

E.  Edit the INI file, and set a path for the Inventory files to a location where you have update rights.  Typically any location other than C:\Program Files.  For example, "C:\data\jobs\Inventory.md5":

;FileChangeINI.MD5 Configuration File
FileDirectory     = C:\Temp             //Search this directory
FileMask          = *.*
LastRunInventory  = C:\Data\Jobs\Inventory.MD5
InventoryReport   = C:\Data\Jobs\InventoryReport.MD5
UpdateInventory   = TRUE


with this, the executable can live in C:\Program Files, and the inventory files can be directed to a folder where your normal Windows account has update rights -- such as "C:\Data\Jobs\Inventory.MD5"

F.  Launch with this command line:

C:\Program Files\Util\FileChangeMD5.exe  C:\Data\Jobs\FileChangeINI.MD5

The Executable can live in a protected directory.
The INI is in a known location, anywhere on the disk.  It points to an updateable area.
The Last-run Inventory file lives where you have rights
The searched-File-Directory (C:\Temp) can be anywhere.

This solves the restrictive Windows security, without having to grant rights to odd places.



Command Line Options

This utility is best run from a DOS Command prompt and is designed to be run in a batch, automated, mode.  Except for the single-file report, the program will not pause or stop for user input.

A configuration file (FileChangeINI.MD5) controls how it behaves and where results are written.  All filenames can be pathed.  For example, the program can be configured to look at a server's UNC path, and store the inventory reports on a different drive, and the INI file could live on a third drive, as long as all are accessible by the user running the code.

Each time it runs, it overwrites previous inventories and reports.

Command line options:

FileChangeMD5.EXE  (no parameters)

     Assumes the current directory contains the default-named
     FileChangeINI.MD5 configuration file.

    If FileChangeINI.MD5 does not exist, it will generate a default INI.
    The INI will point to c:\Temp as a sample.

    This assumes your Windows account has update rights in this folder.


FileChangeMD5.exe  C:\Path\FileChangeINI.MD5

    Launches using a manually-specified configuration / INI file
    The INI file does not have-to-have an .MD5 extension, but is recommended.
    If specified on the command line, the INI file can be any name, any extension.


FileChangeMD5.exe  C:\Path\FileChangeINI.MD5 FALSE

     Runs in test, but does not make changes to the Inventory.


Possible Command Line Error:

When trying to run a single-file MD5 check, it is easy to forget the keyword "file=".  The result is messy and the program will be confused, reporting "Warning: Unexpected value in prefs INI..."

Click for larger view
Bad Command line: 
FileChangeMD5.exe  C:\somefile.ext

Proper command line:
FileChangeMD5.exe  file="C:\somefile.ext"




I hope you enjoy using this program.  Drop me a note and tell me how you like it.

Version history:
1.00 - Initial release
1.01 - Added command-line /?  and /help logic
          Improved bad INI file detection with clearer error messages
          Improved missing file= parameter detection.
          Improved error message texts for Configuration/INI errors


Of Interest:
A one pixel change in a photograph, changing a white pixel to light grey, resulted in this MD5 change:

MD5: 9f-46-58-ef-3d-fe-76-45-65-61-f0-d3-a7-f3-62-bc     Original File; saved twice
MD5: 54-d3-9d-69-07-1a-9f-d9-92-1c-1e-2b-54-42-de-a4   One-pixel change


Related Keyliner Programs:
BullDozer -- a batch file delete program
Prize Select Raffle Program

No comments:

Post a Comment

Comments are moderated and published upon review.