Perhaps something like the following?
First, I mocked up your input file as you didn't give an example:
= a.txt = c.txt = b.txt = e.txt
Here's the example code. Note I've changed your directory handle to a lexical one as opposed to a bareword global. Comments inline:
use warnings; use strict; my $directory_path = '.'; opendir my $dir, $directory_path or die "Cannot open directory $direct +ory_path"; # create a hash with the files in the dir # (makes lookup faster later on) my %directory_path_file_list = map { $_ => 1 } readdir $dir; closedir $dir; my $text_file = "text_file_with_names.txt"; open (my $fh, '<', $text_file) or die("Can't open input file \"$text_file\": $!\n"); # define a new array for the files listed in the file, # extract the names, and push each filename onto the new # array my @files_in_file; while (my $line = <$fh>) { if ($line =~ /=\s+(\w+\.txt)/){ push @files_in_file, $1; } } # loop over the files found in the file, then check to see # if each one exists in the hash we created earlier. # you can perform your actions inside of the 'if' statement for (@files_in_file){ if (defined $directory_path_file_list{$_}){ print "$_ file exists on disk\n"; } else { print "$_ file doesn't seem to exist\n"; } }
Update: It slipped my mind to use -e as Eily suggested, but that's the way I'd go as well.
In reply to Re: Gather List of Files from Text - Compare Against List of Files from Directory
by stevieb
in thread Gather List of Files from Text - Compare Against List of Files from Directory
by Nico
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |