The problem is here, I think:
foreach my $file (readdir D) { my ($filebase, $dirname, $ext) = fileparse($file, '\..*'); my $csvFile = "$opts{d}\\$filebase.csv"; open FILE_OUT, ">$csvFile" or die ...;
You need to filter the set of file names coming back from readdir, and not process every one of them. You are adding files to the directory on every iteration of the loop, and I believe those new files will be returned by readdir on subsequent iterations.

Since you are not paying attention to what $ext is, you will probably get a $file with the value "filename.csv" (which had just been written on some previous iteration) and reopen it for output (truncating it to 0 bytes).

Presumably, the logic to read the "input" file follows the open FILE_OUT, ">$csvFile", and so at the point when you try to read data from that "input" file, there isn't any. (Update: and even if there were still data in the file, it would not be in the format that you are expecting as input. What would the rest of your code do in that case?)

Let's suppose that there is a consistent file extension that identifies all the proper input files (e.g. maybe it's something like ".txt"); then your loop should start like this:

for my $file ( grep /\.txt$/, readdir D )
or like this:
for my $file (<$opts{d}/*.txt>) # use a file glob
so that you never end up trying to input the "csv" files that you are creating as output. Or you could make sure to write your output files to a different directory (that's an approach I would prefer, personally).

In reply to Re: readdir and print file issue by graff
in thread readdir and print file issue by eide

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.