Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- use strict;
- use File::Next;
- use Cwd;
- my $write_to_path = "$ENV{HOMEDRIVE}$ENV{HOMEPATH}";
- my $answer;
- my $path;
- my $ext;
- my $write;
- print "\nFile Finder Version 1.02\n";
- print "\n---------- Author: Hassan Shahid ---------\n";
- print "First, please enter a path to begin in for the search.\n";
- print "The higher up the directory tree, the more results you'll receive.\n";
- WHOLEPROGRAM:
- while (1) {
- LOOPFORPATH:
- while (1) {
- print "\nPlease enter the path to look at: ";
- chomp ($path = <STDIN>);
- print "path: $path --- Are you sure (y/n)?: ";
- chomp ($answer = <STDIN>);
- if (! -d "$path") {
- print "ERROR!!! The path you entered doesn't exist";
- next LOOPFORPATH;
- }
- last LOOPFORPATH if ($answer eq 'y');
- next LOOPFORPATH if ($answer eq 'n');
- }
- print "Now, please enter a filename/string to filter for.\n";
- print "To look just for an extension: (example) \\.pdf --- This will return files like: test.pdf, test.pdf.bk\n";
- print "For spefic final extensions, (example) \\.pdf$ --- This will return files like: test.pdf\n";
- LOOPFOREXT:
- while (1) {
- print "\nPlease enter the search filter: ";
- chomp ($ext = <STDIN>);
- print "Filter: $ext --- Are you sure (y/n)? ";
- chomp ($answer = <STDIN>);
- last LOOPFOREXT if ($answer eq 'y');
- next LOOPFOREXT if ($answer eq 'n');
- }
- LOOPFORFILE:
- while (1) {
- print "Do you want to write the results to a file? (y/n): ";
- chomp ($write = <STDIN>);
- if ($write eq 'y') {
- INNERFILE:
- while (1) {
- print "Output file destination: $write_to_path --- Are you sure (y/n)?: ";
- chomp ($answer = <STDIN>);
- if (! -d "$write_to_path") {
- print "ERROR!!! The path you entered doesn't exist\n";
- next INNERFILE;
- }
- last INNERFILE if ($answer eq 'y');
- if ($answer eq 'n') {
- 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) ";
- chomp ($write_to_path = <STDIN>);
- if ($write_to_path eq '') {
- $write_to_path = Cwd::cwd();
- }
- goto INNERFILE;
- }
- }
- open (OUTPUT, ">$write_to_path/file_finder_results.txt") or die "$!\n";
- last LOOPFORFILE;
- }
- last LOOPFORFILE if ($write eq 'n');
- }
- my $iter = File::Next::everything({file_filter => sub { /$ext/} }, $path);
- #my $iter = File::Next::files($path);
- while (defined (my $file = $iter->())) {
- #next if ($file !~ /\.$ext$/);
- print "$file\n";
- if ($write eq 'y') {
- print OUTPUT "$file\n";
- }
- }
- if ($write eq 'y') {
- close OUTPUT;
- }
- print "Do you want to exit? (y/n) (if 'n', the program will start again for a new search)\n";
- chomp (my $exit = <STDIN>);
- last if ($exit eq 'y');
- if ($exit eq 'n') {
- print "OK, Starting again!\n\n";
- next;
- }
- }
Add Comment
Please, Sign In to add comment