fork download
  1. #!/usr/bin/perl
  2. use File::Find;
  3. use File::Basename;
  4. use File::Copy;
  5.  
  6. $HOME_dir = `echo \$HOME`;
  7. chomp $HOME_dir; #刪去換行符
  8. $dst_dir = $HOME_dir."/Desktop/tmp/"; #目的路徑
  9. $pic_dir = $HOME_dir."/magic/dir"; #圖片檔目錄
  10. $text_filename = "list2.txt";
  11.  
  12. sub find_file {
  13. $dir = $File::Find::dir;
  14. $fname = $File::Find::name;
  15. $name = $_;
  16. $basename = fileparse($fname, qr/\..*/);
  17. next if ($name =~ /^\./);
  18. next if (-d $name);
  19. $num = $basename;
  20. if ($num ne " ") {
  21. $HASH{$num} = $fname;
  22. }
  23. }
  24. #印出HASH內容(用於驗證)
  25. sub print_hash_data {
  26. print "-----Print HASH Data-----\n";
  27. while(($key, $value) = each %HASH) {
  28. print "$key => $value\n";
  29. }
  30. }
  31. sub print_file_count {
  32. $command = "find $pic_dir -type f | wc -l";
  33. $count = `$command`;
  34. print "Total find files:$count\n";
  35. }
  36. #測試tmp目錄是否存在,如果不在就建立目錄
  37. sub test_tmp_dir {
  38. mkdir($dst_dir) unless (-d $dst_dir);
  39. }
  40. #比對 text.txt 和HASH 是否符合並copy到tmp目錄
  41. sub compare_and_copy {
  42. open FILE, $text_filename or die "Can't open $text_filename";
  43. while (<FILE>) {
  44. print "Part Num:$_\n";
  45. $p_num = $_;
  46. if (exists ($HASH{$p_num})) {
  47. $pic_name = $HASH{$p_num};
  48. $pic_basename = basename($pic_name);
  49. print "Pic name:$pic_basename\n";
  50. $newfile = $dst_dir.$pic_basename;
  51. print "\tCopy to:$newfile\n";
  52. copy ($pic_name, $newfile);
  53. } else {
  54. print "\t---No math!---\n";
  55. }
  56. }
  57. }
  58. find(\&find_file, $pic_dir); #執行遞迴
  59. #print_hash_data();
  60. test_tmp_dir();
  61. compare_and_copy();
  62. print_file_count();
  63.  
Runtime error #stdin #stdout #stderr 0.03s 8264KB
stdin
Standard input is empty
stdout
Standard output is empty
stderr
Can't open list2.txt at prog.pl line 42.