Monday, August 6, 2012

Calculating md5dum of all files in a folder

# Function Name : md5sum_dir
# Input            : Path of folder
# Output        : none
# Description    : This function will recursively check md5sum of all files under passed folder
# Created By    : Rinkesh Bansal
#
sub md5sum_dir ($)
{
    my $path = shift;

    opendir (DIR, $path) or die "Unable to open $path: $!";
    my @files = map {$path . '/' . $_ }grep { !/^\.{1,2}$/ } readdir (DIR);
    closedir (DIR);

    foreach my $file (@files)
    {
        if (-d $file)
        {
            &md5sum_dir ($file);
        }
        else
        {
            my $md = &md5sum($file); # you can find this subroutine in my other blogs
            open (FO, ">>$result_file") or die ("Error :: Couldn't open file ($result_file) for writing\n");
            print FO "$file\t$md\n";
            close(FO);
        }
    }
}

No comments:

Post a Comment