• We’re currently investigating an issue related to the forum theme and styling that is impacting page layout and visual formatting. The problem has been identified, and we are actively working on a resolution. There is no impact to user data or functionality, this is strictly a front-end display issue. We’ll post an update once the fix has been deployed. Thanks for your patience while we get this sorted.

how to verify multiple files exist in perl

JohnnyMCE

Member
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?
 
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:
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;
 
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.
 
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:
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.
 
Back
Top