how to verify multiple files exist in perl

JohnnyMCE

Member
Apr 13, 2006
141
0
0
this might be a remedial question but I am very new to perl hence why I am having a problem. I know the basic way to check for one file.

#!/usr/bin/perl -w

$filename1 = '//network_share/file.TXT;

if (-e $filename1) {print "File Exists!"}
else {print "File does not exist!"}

How would i go about checking for another file like filename2 inside the same script?
 

Crusty

Lifer
Sep 30, 2001
12,684
2
81
My perl is REALLY rusty so I'm sure I'm messing up the syntax but....

Code:
@files = ('filename1','filename2','filename3')
for $i (0 .. $#files) {
  print "File $files[$i] "
  if (-e $files[$i]) {
    print 'does not exist.'
  }
  else {
     print 'exists.'
   }
}
 
Last edited:

Aluvus

Platinum Member
Apr 27, 2006
2,913
1
0
Code:
@files = qw/file1 file2 file3/;
foreach (@files)
{
    if (-e $_)
    {
        print "File '$_' Exists!\n";
    }
    else
    {
        print "File '$_' does not exist!\n";
    }
}

A more flexible approach would be to allow the array of file names to be set from the command line:

Code:
@files = @ARGV;
 

Ken g6

Programming Moderator, Elite Member
Moderator
Dec 11, 1999
16,566
4,481
75
I generally prefer -f (exists and is a file, as opposed to a directory) to -e (exists). But that may vary depending on your application.
 

Gooberlx2

Lifer
May 4, 2001
15,381
6
91
I do this a lot at work, with hundreds or thousands of paths at a time. I usually have a simple text list of IDs or paths, or whatever...

Code:
$list = "/path/to/list.txt";
use Tie::File;
tie @files, "Tie::File", $list or die "failed, does your list exist?";

foreach $file (@files){
	if(-f $file){
        	print $file." exists\n";	
	}
}
 
Last edited:

bhas85

Junior Member
Jun 26, 2012
2
0
0
Hi All,

I am new to perl i had a doubt.

like i had 10 file in one directory and i had text file which contains same files.The thingis i need to compare the files in the text file and in directory.If anything is missing i need to send mail.
Can any please help how to compare i came to know how to read file.But didn't understand how compare from directory.

If you had please let me know.