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

Hello, I have a directory with multiple files. I'd like to create a new output file for each input file. I'm sure there is a good example somewhere here, but I haven't been able to find it through the search functions. Can someone point me to one? -William
  • Comment on How to create an output file for each input file

Replies are listed 'Best First'.
Re: How to create an output file for each input file
by apl (Monsignor) on Apr 02, 2008 at 20:17 UTC
    What exactly do you mean when you say you want to create a new output file for each input file? Will the output file contain selected lines from the input file? How will the output file be named, viz a viz the input file? Which search functions did you look at?

    And most importantly, what have you written to try to implement a solution to your question?

      I'm trying read each file in a directory, change some of the text, then write it to a new file in another directory.

      Here's what I've written:

      #get the names of all .tab files in inbox directory opendir(DIR,$inbox); @files = grep(/\.TAB$/,readdir(DIR)); closedir(DIR); ## loop through the TAB files### foreach $file (@files) { chomp; $verifile = $outbox.$yr.$mn.$day.$hr.$min.$sec."_FIXED.TAB"; open(OUTPUT_FILE, ">>$verifile")||die "Can't open file: $!"; #string manipulation will go here close OUTPUT_FILE; }# end for each file

      Edit: g0n - code tags

        A few more questions:
        • Do the existing files end .TAB or .tab?
        • You will need to define $inbox and $outbox.
        • You will need to add something to the value of $verifile to reflect $file. (That is, /old/WARRANT.TAB should result in /new/WARRANT_080402_164500_FIXED.TAB which requires some separators as well)