So;
I have a list in a text file that contains sections of a what would be filenames..

I'm wanting to read from this text file into I guess an array, unless there's a better way to do it.. and then find the file that matches with each of the entries in the array and perform an action against that file....

Text file
aaa.exe
bbb.exe
ccc.exe
ddd.exe

File list
aaa.exe
ggg.exe
ccc.exe
rrr.exe

So how would I read in the entries of the text file, read in the entries of the *.exe files in a specified directory.. search the *.exe's for matches and then perform an action against them?

Recommended Answers

All 4 Replies

This is what I've come up with so far :-/

my $directory = 'C:\testingdir';

opendir(DIR,$directory);
open (REMAINING, 'C:\testingdir\testing.txt');
my $remaining_patch_names;
my @files = readdir(DIR);
foreach my $name (@files)
{
  while ($remaining_patch_names = <REMAINING>)
       {
            chomp($remaining_patch_names); #remove new-line character from end of $remaining_patch_names
            my $qmname = quotemeta($remaining_patch_names);
            if ($name =~m/$qmname/i)
            {
                print "***FOUND*** $name \n";
            }
            else
            {
                print "***NOTFOUND*** $name \n";
            }
       }
       
}

This seems to be working.. but is there a better way?

my $directory = 'C:\testing';
opendir(DIR,$directory);
open (REMAINING, 'C:\testing\testing.txt');
my $remaining_patch_names;
my @files = readdir(DIR);
my @patches = <REMAINING>;
foreach my $patch (@patches)
{
 chomp($patch);
 foreach my $name (@files)
 {
  chomp($name);
  if ($patch =~m/$name/i)
            {
                print "***FOUND*** $patch \n";
                last;
            }
            else
            {
                
            }
 }
        
}
#!/usr/bin/perl
#CheckFilesExist.pl
use 5.006;
use strict;
use warnings;
my $directory = '/home/david/Documents';
open (my $fh, '<', 'files.txt');
while (<$fh>) {
	chomp;
	if (-e $directory . '/' . $_) {
		print "***FOUND*** $_ \n";
	}
	else {
		print "***NOTFOUND*** $_ \n";
	}
}

The following incorporates a couple of improvements on the above, including open (my $fh, '<', $filenames) or die "Could not open $filenames $!";

#!/usr/bin/perl
#CheckFilesExist.pl
use 5.006;
use strict;
use warnings;
my $cur_dir = '/users/david/Programming/Perl';
my $dir2check = '/users/david/Documents';
my $filenames = "$cur_dir/files.txt";
open (my $fh, '<', $filenames) or die "Could not open $filenames $!";
while (<$fh>) {
	chomp;
	if (-e $dir2check . '/' . $_) {
		print "***FOUND*** $_ \n";
	}
	else {
		print "***NOTFOUND*** $_ \n";
	}
}
close($fh);
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.