Hi Monks, I need some wisdom with how to quickly and easily set up a for loop. Here's what I want to do. I have multiple directories, and within each of these directories, there is either a file with a certain extension, say *.txt, or there is not. What I need to do is loop through each of these directories to check if the file that has a *.txt extension exists, and if that file does exist, I need to copy another file within that directory -- let's call that file $out. Here's my code setup:
#!/usr/bin/perl use warnings; $out = "outFile"; $newdir = $newdirectory; open(TABLEA, "dirlist"); @tablea = <TABLEA>; foreach $dir (@tablea) { chdir $dir; if (glob("*.txt")) { system("mv $out $newdir"); } }
So, the directory list is set as an array, @tablea, and each directory is called $dir. If the file w/ extension *.txt exists, mv the $out file (within that directory) to the new directory, $newdir. Repeat the check for each directory. If run this script, nothing gets sent to new directory, so I was wondering how I could improve this script so that this script runs correctly? Thanks.

In reply to Check if file exists in multiple directories by Bama_Perl

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.