Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

Calc md5 for file by drag and drop (Windows)

by GrandFather (Saint)
on Oct 14, 2005 at 02:32 UTC ( [id://500108]=CUFP: print w/replies, xml ) Need Help??

When set up to be accessed from a desktop short cut ("perl.exe CalcMD5.pl" in the shortcut target field) this script calc the md5 for a file dropped onto the short cut and pastes the result into the clipboard

use warnings; use strict; use Digest::MD5::File qw(file_md5); use Win32::Clipboard; my $myfile = shift; my $md5 = new Digest::MD5; $md5->addpath($myfile); my $digest = $md5->hexdigest; Win32::Clipboard::Set ($digest); print $digest;

Perl is Huffman encoded by design.

Replies are listed 'Best First'.
Re: Calc md5 for file by drag and drop (Windows)
by fizbin (Chaplain) on Oct 18, 2005 at 07:22 UTC
    A nice enhancement of this would be to see if the current Clipboard text matched /^\s*[a-f\d]{32}\s*$/i and, if so, popping up a nice MsgBox saying whether the file dropped had the same md5 as what was in the clipboard.

    Or something more elaborate, such as parsing the clipboard text for stuff that looks like:

    MD5 (example_1) = bea8252ff4e80f41719ea13cdf007273 MD5 (example_2) = e0675e728056818b0392c0c2f5478ff0
    (Or whatever format is used by the md5 program all the cool kids are using these days) And then forming relative filenames so that one just has to copy a chunk of MD5 statements like the above, and drag one of the files in the directory over onto the icon.

    Note that another fine use of the script as-is is to drop a shortcut to it in one's SendTo folder in the %USERPROFILE% directory, or added to the context menu of every file by loading a registry file such as:

    Windows Registry Editor Version 5.00 [HKEY_CLASSES_ROOT\*\shell] [HKEY_CLASSES_ROOT\*\shell\Calc MD5] [HKEY_CLASSES_ROOT\*\shell\Calc MD5\command] @="C:\\Perl\\Perl.exe c:\\where\\ever\\CalcMD5.pl \"%1\""

    Update: I've now gone and investigated the output of various md5 utilities, and have this to report:
    • md5sum on most linux distributions produces this format: (i.e. two spaces)
      9c4ec476409f3a02f3ffb337a91cb65f mud.el
    • md5 on my Debian box does this:
      9c4ec476409f3a02f3ffb337a91cb65f mud.el
      where what's between the sum and the filename is a single character number 9. (tab)
    • md5sum on BSD boxes does this:
      MD5 (will.zip) = 9c05488e1ccfe1b77c33b12706fa8151

    Hopefully, covering that mess will cover all that's needed.

    --
    @/=map{[/./g]}qw/.h_nJ Xapou cets krht ele_ r_ra/; map{y/X_/\n /;print}map{pop@$_}@/for@/
Re: Calc md5 for file by drag and drop (Windows)
by blazar (Canon) on Oct 18, 2005 at 07:52 UTC

    Nice! But...

    use Digest::MD5::File qw(file_md5);

    Ok, but then you're using Digest::MD5 instead, which indeed is loaded by Digest::MD5::File. You may just either use the former since it's a core module now or take advantage of the latter:

    my $myfile = shift; my $md5 = new Digest::MD5; $md5->addpath($myfile); my $digest = $md5->hexdigest; Win32::Clipboard::Set ($digest);

    I would just either

    Win32::Clipboard::Set Digest::MD5->new->addpath(shift)->hexdigest;

    or

    Win32::Clipboard::Set file_md5 shift;

    in the two respective cases hinted above.

    Oh, and I wouldn't print $digest; since we're putting that info in the clipboard anyway, but I would use wperl.exe instead. Of course an alternative would be a minimal GUI (e.g. Tk) showing the computed checksum, possibly along with a button to copy to the clipboard or compare with a value previously stored into it. However this would loose the fascinating simplicity/minimality of your solution...

      This was something I wipped up quickly at work when I needed to calculate a digest and knew that I would need to do it again occassionally. I posted it here having come in at the tail end of a CB converstaion and thought it might help the person asking the the MD5 related question so it got posted in a hurry too.

      I'm pleased to see it's been so popular and I appreciate the feedback. I'll put together a more fully featured version at some point adding in your suggestions and fizbin's ideas and the ability to calc the MD5s for a folder full of files too. The code won't be quite so compact, but usage can remain similar.


      Perl is Huffman encoded by design.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: CUFP [id://500108]
Approved by planetscape
Front-paged by grinder
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others perusing the Monastery: (7)
As of 2024-04-18 17:41 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found