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!

testing for multiple files 2

Status
Not open for further replies.

MoshiachNow

IS-IT--Management
Joined
Feb 6, 2002
Messages
1,851
Location
IL
HI,

I guess it's an easy one.

I have multiple files in my dir like At21.job,At13.job,etc

I need to test for at least one file of this type existence.
The following line does not seem to do the job:

unless (-f "<At*.job>") {

I have also tried: unless (-f "At*.job") {

and : unless (-f "At.*") {


Appreciate any ideas.



Long live king Moshiach !
 
try
Code:
if(-e "At*.job"){ exits , do what ever }

also I always use the entire path when i use file operators, don't know if it makes a difference.

i.e,
Code:
my $file = "PATH_TO_FILE/At*.job";
if(-e $file){do whatever};

"In complete darkness we are all the same, only our knowledge and wisdom separates us, don't let your eyes deceive you.
 
how about....
Code:
# get all files 
my @files = glob('DIR_TO_FILES/*');
my $OK = 0;
# Loop 
for(@files){
   if($_ =~ m/At/gi){  $OK = 1; }
}

if($OK){ Do what ever }

"In complete darkness we are all the same, only our knowledge and wisdom separates us, don't let your eyes deceive you.
 
also to make it more efficient as there is no point in continuing once match is made..
Code:
 # get all files 
my @files = glob('DIR_TO_FILES/*');
my $OK = 0;
# Loop 
for(@files){
   if($_ =~ m/At/gi){  $OK = 1; last;}
}
if($OK){ Do what ever }

"In complete darkness we are all the same, only our knowledge and wisdom separates us, don't let your eyes deceive you.
 
probably, this is a case of the blind leading the blind i'm afraid ;-)

also note that my example will match 'At' anywhere, that includes any part of the file path as well as name!

So ensure none of your directories have 'At' in them.

I'm sure one of the PERL guru's will be able to offer a 'better' solution, I guess the -e with the correct syntax should do it, but maybe you can't use wild cards the way you want to.


Also I noticed the example i gave ring case, so should be.
Code:
if($_ =~ m/At/g){  $OK = 1; last;}

if you are any good at regexe's you might also want to make it a better match like so...
Code:
if($_ =~ m/At[^>]*.job/g){  $OK = 1; last;}
I think that's correct to say must match 'At'..[Anything]'.job' , but i'm not that great at regex's



"In complete darkness we are all the same, only our knowledge and wisdom separates us, don't let your eyes deceive you.
 
or maybe...
Code:
if($_ =~ m/At+[0-9].job/g){  $OK = 1; last;}
to match 'At'[numbers]'.job'


"In complete darkness we are all the same, only our knowledge and wisdom separates us, don't let your eyes deceive you.
 
This might help:
Code:
chdir 'c:/test/';
print "$_\n" for <At*.job>;
 
Try a grep:

Code:
my $files = grep (/At[0-9]+\.job/, glob('DIR_TO_FILES/*'));

if ($files == 0) {
   print "no files matched";
}
else {
   print "there were $files files";
}

$files would return true all the time as long as it's not zero, so that

Code:
if ($files) {

would return true only if there was at least one file found.
 
lol PERL, the beauty of more than one way to do it, now where did I put the cat?

"In complete darkness we are all the same, only our knowledge and wisdom separates us, don't let your eyes deceive you.
 
lol PERL, the beauty of more than one way to do it, now where did I put the cat?
Definitely the best Perl quote I've heard in months.
 
Thanks Ishnid, i'm suprised you didn't add your 2 pence worth on how you'd have done this ;-)

"In complete darkness we are all the same, only our knowledge and wisdom separates us, don't let your eyes deceive you.
 
Is that an invitation? ;-)

Probably something like:

Code:
if ( glob '/path/to/At*.job' ) {
   # at least one exists matching that pattern
}
 
knew you had it in you!

"In complete darkness we are all the same, only our knowledge and wisdom separates us, don't let your eyes deceive you.
 
You can write faster code than perl, but you can write code faster with perl."

My perennial favourite.
-BG

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top