Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Checking if one or more files exist

Status
Not open for further replies.

jayjaybigs

IS-IT--Management
Jan 12, 2005
191
CA
Hello All,

I am trying to check if multiple file exist in a directory.

Here is example:

if( -e $myfile){

print "The file $myfile exist in the directory}

else{

die "Could not find any file in this directory"\n;
}

However, I want to check that not does $myfile exist but it also has to be only one $myfile. Else die and exit with

print "More than one file exist in the directory\n";

Please help to acomplish this.
Thanx
 
Code:
if( -e $myfile){
  print "The file $myfile exist in the directory\n";
} else {
  die "Could not find any file in this directory\n";
}
while this code will run, it's not correct.
The test for $myfile, will not test for $yourfile, which could also exist in the same directory. Unless you're working on VMS, you won't find two files with the same name in any directory (even then they'll have version numbers)

you'll need to look into opendir, readdir, and closedir to find the contents of a specified directory. you could also do it with globbing <*>

you'd be best to have a look at a few methods to understand how they work

--Paul

cigless ...
 
what operating system are you using that allows,
as I understand your posting, multiple files of
the same name to co-exist in the same directory/folder?

or, is the some sort of a regex in the file name?
 
Are mean that you have files such as:

myfile.123.txt
myfile.234.txt
myfile.345.txt

in a directory?

If this is the case there are several answers.

1) glob

Grab all file of the pattern 'myfile.*.txt'.

Code:
my @myfiles = glob('/my/dir/myfile.*.txt');
if( my $cnt = @myfiles ){
  print "The file $myfile exist in the directory $cnt times.\n";
} else {
  die "Could not find any file in this directory.\n";
}

2) grep/glob

Grab only those files that match the pattern 'myfile.*.txt', where the wildcard is only a number.

Code:
my @myfiles = grep /myfile.\d+.txt/, glob('/my/dir/myfile.*.txt');
if( my $cnt = @myfiles ){
  print "The file $myfile exist in the directory $cnt times.\n";
} else {
  die "Could not find any file in this directory.\n";
}

3) File::Find::Rule

Search from the named directory and find all files of the pattern 'myfile.\d+.txt', using the CPAN module File::Find::Rule (1).

Code:
use File::Find::Rule;

my @places = qw(
	/my/dir
);

my @myfiles = File::Find::Rule->file()->name( qr/myfile.\d+.txt/ )->in( @places );
if( my $cnt = @myfiles ){
  print "The file $myfile exist in the directory $cnt times.\n";
} else {
  die "Could not find any file in this directory.\n";
}

Hope that gives you some idea of where to start.

(1) cpan.org appears to be having problems, so you can also find information here: [URL unfurl="true"]http://cpan.uwinnipeg.ca/module/File::Find::Rule[/url].

Barbie
Leader of Birmingham Perl Mongers
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top