1 | ppm install Digest::MD5 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | sub INPUT_FOLDER { print "lease input the original binary folder path!\n"; chomp( $folder_path = <STDIN> ); if ( -e $folder_path ) { #输入文件夹路径存在性判定 my @dir_files = <$folder_path/*>; if (@dir_files) { #输入文件夹非空性判定 push @ori_folder, $folder_path; opendir DH, $folder_path || die "Can't open @ori_folder!"; @ori_file = readdir DH; splice( @ori_file, 0, 2 ); @ori_file = sort @ori_file; &BANNER; &INPUT_ANOTHER_FOLDER; } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 | sub INPUT_ANOTHER_FOLDER { $h++; print "ress Q to quit the tool!\n"; print "ress C to end input and start to compare MD5 value!\n"; print "lease input another folder path compare with the original folder:\n"; while (<STDIN>) { chomp $_; if (/^q$/i) { exit; } elsif (/^c$/i) { if ( $h == 2 ) { &BANNER; print "There should be at least 2 folders to be compared!\n"; $h--; &INPUT_ANOTHER_FOLDER; } else { &BANNER; &FOLDER_ESTIMATE; } } else { push @com_folder, $_; if ( $h == 5 ) { #程序设定最大只能同时比较 5 个路径下面的文件 &BANNER; print "You have entered 5 folders to be compared!\n"; &FOLDER_ESTIMATE; } else { &BANNER; &INPUT_ANOTHER_FOLDER; } } } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 | #待比较文件夹存在性判定 foreach (@com_folder) { if ( -e $_ ) { push @folder_exist, $_; } else { push @folder_not_exist, $_; } } #待比较文件夹非空性判定 foreach (@folder_exist) { my @dir_files = <$_/*>; if (@dir_files) { push @folder_not_empty, $_; } else { push @folder_empty, $_; } } #待比较文件夹内文件数目判定 foreach (@folder_not_empty) { opendir DH, $_ || die "Can't open @folder_not_empty!"; @com_file = readdir DH; splice( @com_file, 0, 2 ); @com_file = sort @com_file; my $file_count = @com_file; if ( $file_count == @ori_file ) { push @folder_equal, $_; } else { push @folder_not_equal, $_; } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | sub MD5_COM { print "Start to read and compare the input folder binary MD5 value, Please wait...\n\n"; #批量读取初始文件夹内所有文件 MD5 值 foreach (@ori_file) { my $file = "$folder_path\\$_"; open( FILE, $file ) || die "Can't open '$file'!"; binmode(FILE); my $md5 = Digest::MD5->new; map { $md5->add($_) } <FILE>; close(FILE); push @ori_array, "$file\n", $md5->hexdigest . "\n"; } #将 MD5 值及发生的错误信息打印输出到日志文件 open RESULT, ">MD5.log"; print RESULT "@folder_not_exist are not exist!\n" if @folder_not_exist; print RESULT "@folder_empty have no files!\n" if @folder_empty; print RESULT "@folder_not_equal have the wrong file numbers!\n" if @folder_not_equal; print RESULT "Original folder:\n", @ori_array, "$line\n"; |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 | sub Com_folder1 { #读取第一个待比较文件夹内所有文件 MD5 值 opendir DH, $folder_equal[0] || die "Can't open @folder_equal!"; @com_file = readdir DH; splice( @com_file, 0, 2 ); @com_file = sort @com_file; foreach (@com_file) { my $file = "$folder_equal[0]\\$_"; open( FILE, $file ) || die "Can't open '$file'!"; binmode(FILE); my $md5 = Digest::MD5->new; map { $md5->add($_) } <FILE>; close(FILE); push @com_array1, "$file\n", $md5->hexdigest . "\n"; } #屏幕输出 MD5 值比对结果并保存到日志文件 my $pass_count = 0; my $fail_count = 0; for ( my $n = 0 ; $n < $#ori_array ; $n += 2 ) { if ( $ori_array[ $n + 1 ] eq $com_array1[ $n + 1 ] ) { $pass_count++; } else { $fail_count++; } } my $total = $pass_count + $fail_count; print "\nCompare the MD5 value of:\n\n"; print "$folder_equal[0]\t$folder_path\t"; print "ASS $pass_count\tFAIL $fail_count\tTotal $total\n\n"; } |
欢迎光临 电子技术论坛_中国专业的电子工程师学习交流社区-中电网技术论坛 (http://bbs.eccn.com/) | Powered by Discuz! 7.0.0 |