fork download
  1. #!/usr/bin/perl
  2. use File::Find;
  3. use File::Copy;
  4. use File::Basename;
  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. $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 (exists ($HASH{$num})) {
  21. print "find!!\n";
  22. $newfile = $dst_dir.$name;
  23. print "File: $fname\n";
  24. print "Dst:$newfile\n";
  25. if (-d $dst_dir) {
  26. copy ($fname, $newfile);
  27. } else {
  28. mkdir($dst_dir);
  29. copy ($fname, $newfile);
  30. }
  31. }
  32. } #sub END
  33. sub print_hash_data {
  34. print "-----Print HASH Data-----\n";
  35. while(($key, $value) = each %HASH) {
  36. print "$key => $value\n";
  37. }
  38. }
  39. sub print_file_count {
  40. $command = "find $pic_dir -type f | wc -l";
  41. $count = `$command`;
  42. print "Total find files:$count\n";
  43. }
  44. #測試tmp目錄是否存在,如果不在就建立目錄
  45. if (!-d $dst_dir) {
  46. mkdir($dst_dir);
  47. }
  48. $line_num = 1;
  49. open FILE, $filename or die "Can't open $filename";
  50. while (<FILE>) {
  51. print "Part:$_\n";
  52. $p_num = $_;
  53. $HASH{$p_num} = $line_num;
  54. $line_num ++;
  55. }
  56. print_hash_data();
  57. find (\&find_file, $pic_dir); #執行遞迴找圖片檔案
  58. print_file_count();
  59.  
Runtime error #stdin #stdout #stderr 0.04s 7716KB
stdin
Standard input is empty
stdout
Standard output is empty
stderr
Can't open list2.txt at prog.pl line 49.