Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

Search a directory for files

by gzayzay (Sexton)
on Mar 09, 2006 at 20:20 UTC ( [id://535489]=perlquestion: print w/replies, xml ) Need Help??

gzayzay has asked for the wisdom of the Perl Monks concerning the following question:

Hi Monks,

I am trying to search a directory for two files I have created. If those files are found I will like to perform a particular task. However, If the files are not found, I will like to call a function that generates those files am searching for.

My problem is, I am having difficulties searching the directory to see if the file exist. I have a "for" loop as follows:

if (opendir (KDBASE, $path)) { @keywords = sort grep{$_ ne '.' and $_ ne '..'} readdir (KDBASE); for ($kdbase = 0; $kdbase <= $#keywords; $kdbase++) { if ( @keywords[$kdbase] !~ /database\.txt|keywords_databas +e\.txt/) { next; } elsif (@keywords[$kdbase] =~ /keywords_database\.txt/) { $keyword_database_present = 1; print "Keyword database found : @keywords[$kdbase]\n"; } elsif(@keywords[$kdbase] =~ /database\.txt/) { $testfilelist_database_present = 1; print "Test file list database found : @keywords[$kdba +se]\n"; } else { print "No database found yet : @keywords[$kdbase]\n"; #Generate all the necessary files. GenerateTestFileList(); } } }

I wiil like to search the entire folder first to see if my files are present. If not, then I will like the GenerateTestFileList() function to be called. What is currently happening with the above code, when every it searches the first file in the directory and it is not the file I am looking for, it calls the GenerateTestFileList() function. However, I want to search the entire directory and ensure that the files does not exit before calling the GenerateFileList() function. The Great monks kindly help me with this task?

Thanks,

Edman

Replies are listed 'Best First'.
Re: Search a directory for files
by davidrw (Prior) on Mar 09, 2006 at 20:27 UTC
    can you just check for the file's existence with -f (see -X)
    my ($file1, $file2) = ("$path/database.txt", "$path/keywords_database. +txt"); -f $file1 or do { # build $file1 here }; -f $file2 or do { # build $file2 here };
Re: Search a directory for files
by philcrow (Priest) on Mar 09, 2006 at 20:25 UTC
    Try the -f or -r tests to look for your files.

    Phil

    A reply falls below the community's threshold of quality. You may see it by logging in.
Re: Search a directory for files
by wfsp (Abbot) on Mar 09, 2006 at 20:34 UTC
    Shouldn't you have
    GenerateTestFileList();
    outside of the loop? Perhaps something like
    GenerateTestFileList() unless $testfilelist_database_present;
      I will definitely try that. Thanks to all Monks for their assistance
Re: Search a directory for files
by swkronenfeld (Hermit) on Mar 09, 2006 at 20:30 UTC
    Update: davidrw's response above is much simpler. I was responding to the algorithm as posted, and pointing out that your inner loop needed some work :)

    You should probably restructure your inner loop, and pull the GenerateTestFileList() call outside of it.
    my $count = 0; foreach (@keywords) { if( ($_ eq "database.txt") || ($_ eq "keywords_database.txt") ) { print "Found $_\n"; $count++; last if($count == 2); } } if($count != 2) { print "At least one database is missing.\n"; GenerateTestFileList(); }
    I would also recommend using the string equality operator (eq) instead of regular expressions. You know exactly what the files are named, and you don't want an accidental match. For example, the strings "random_database.txt" and "some_other_database.txt.tar.gz" will match your elsif(@keywords[$kdbase] =~ /database\.txt/) line above.
      Thanks for that observation. I will consider that change.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://535489]
Approved by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others perusing the Monastery: (9)
As of 2024-03-28 18:59 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found