Guest User

Untitled

a guest
Mar 13th, 2018
141
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.68 KB | None | 0 0
  1. use strict;
  2. use File::Next;
  3. use Cwd;
  4.  
  5. my $write_to_path = "$ENV{HOMEDRIVE}$ENV{HOMEPATH}";
  6. my $answer;
  7. my $path;
  8. my $ext;
  9. my $write;
  10.  
  11. print "\nFile Finder Version 1.02\n";
  12. print "\n---------- Author: Hassan Shahid ---------\n";
  13. print "First, please enter a path to begin in for the search.\n";
  14. print "The higher up the directory tree, the more results you'll receive.\n";
  15.  
  16.  
  17. WHOLEPROGRAM:
  18. while (1) {
  19.  
  20. LOOPFORPATH:
  21. while (1) {
  22. print "\nPlease enter the path to look at: ";
  23. chomp ($path = <STDIN>);
  24. print "path: $path --- Are you sure (y/n)?: ";
  25. chomp ($answer = <STDIN>);
  26. if (! -d "$path") {
  27. print "ERROR!!! The path you entered doesn't exist";
  28. next LOOPFORPATH;
  29. }
  30. last LOOPFORPATH if ($answer eq 'y');
  31. next LOOPFORPATH if ($answer eq 'n');
  32. }
  33.  
  34. print "Now, please enter a filename/string to filter for.\n";
  35. print "To look just for an extension: (example) \\.pdf --- This will return files like: test.pdf, test.pdf.bk\n";
  36. print "For spefic final extensions, (example) \\.pdf$ --- This will return files like: test.pdf\n";
  37.  
  38. LOOPFOREXT:
  39. while (1) {
  40. print "\nPlease enter the search filter: ";
  41. chomp ($ext = <STDIN>);
  42. print "Filter: $ext --- Are you sure (y/n)? ";
  43. chomp ($answer = <STDIN>);
  44. last LOOPFOREXT if ($answer eq 'y');
  45. next LOOPFOREXT if ($answer eq 'n');
  46. }
  47.  
  48. LOOPFORFILE:
  49. while (1) {
  50. print "Do you want to write the results to a file? (y/n): ";
  51. chomp ($write = <STDIN>);
  52. if ($write eq 'y') {
  53. INNERFILE:
  54. while (1) {
  55. print "Output file destination: $write_to_path --- Are you sure (y/n)?: ";
  56. chomp ($answer = <STDIN>);
  57. if (! -d "$write_to_path") {
  58. print "ERROR!!! The path you entered doesn't exist\n";
  59. next INNERFILE;
  60. }
  61. last INNERFILE if ($answer eq 'y');
  62. if ($answer eq 'n') {
  63. print "\nPlease enter the new output file destination: (just press enter if you want it to default to the same folder as the file finder program) ";
  64. chomp ($write_to_path = <STDIN>);
  65. if ($write_to_path eq '') {
  66. $write_to_path = Cwd::cwd();
  67. }
  68. goto INNERFILE;
  69. }
  70. }
  71. open (OUTPUT, ">$write_to_path/file_finder_results.txt") or die "$!\n";
  72. last LOOPFORFILE;
  73. }
  74. last LOOPFORFILE if ($write eq 'n');
  75. }
  76.  
  77. my $iter = File::Next::everything({file_filter => sub { /$ext/} }, $path);
  78. #my $iter = File::Next::files($path);
  79. while (defined (my $file = $iter->())) {
  80. #next if ($file !~ /\.$ext$/);
  81. print "$file\n";
  82. if ($write eq 'y') {
  83. print OUTPUT "$file\n";
  84. }
  85. }
  86.  
  87. if ($write eq 'y') {
  88. close OUTPUT;
  89. }
  90.  
  91. print "Do you want to exit? (y/n) (if 'n', the program will start again for a new search)\n";
  92. chomp (my $exit = <STDIN>);
  93. last if ($exit eq 'y');
  94. if ($exit eq 'n') {
  95. print "OK, Starting again!\n\n";
  96. next;
  97. }
  98. }
Add Comment
Please, Sign In to add comment