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


In reply to Search a directory for files by gzayzay

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.