in reply to Re: How to create an output file for each input file
in thread How to create an output file for each input file

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

Replies are listed 'Best First'.
Re^3: How to create an output file for each input file
by apl (Monsignor) on Apr 02, 2008 at 20:47 UTC
    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)
      Hello, thank you!

      yes, the files end in .TAB and I can define the values for $inbox and $outbox.

      I think where I was getting confused was on the $verifile. Can you suggest what the separators are? Is this what causes a new file to be created each time it loops through?

      Many thanks!
        Rather than

        $verifile = $outbox.$yr.$mn.$day.$hr.$min.$sec."_FIXED.TAB";
        use something like
        $verifile = $outbox."_".$yr.$mn.$day."_".$hr.$min.$sec."_FIXED.TAB";

        (Keep it mind you have to populate the date and time fields.)

        You create the file by opening it. You should look at this tutorial on Open to decide exactly what you want to do.

        Hope it helps !

        ## loop through the TAB files### foreach $file (@files) { chomp; # just add $file so for each $file , there is a $verifile created $verifile = $file.$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