in reply to readdir and print file issue
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.foreach my $file (readdir D) { my ($filebase, $dirname, $ext) = fileparse($file, '\..*'); my $csvFile = "$opts{d}\\$filebase.csv"; open FILE_OUT, ">$csvFile" or die ...;
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:
or like this:for my $file ( grep /\.txt$/, readdir D )
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).for my $file (<$opts{d}/*.txt>) # use a file glob
|
|---|