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

Okay, I figured it out. In my original code snippet, I was creating (what I thought) was a unique file name using the date. But because I was only going down to the second, and the script was running so quickly on test data, it thought it should "append" to the existing file. I redid the naming convention for the outbox file to add the original name of the input file, thereby creating a unique output file after each pass of the foreach loop. The final test code below:

########Test Variables
$outbox = "G:\\outbox\\";
$inbox = "G:\\inbox\\";
$archive = "G:\\archive\\";
$log = "G:\\samslog.txt";


####################get the names of all .dat files in inbox directory
opendir(DIR,$inbox);
@files = grep(/\.TAB$/,readdir(DIR));
closedir(DIR);


################
## loop through the TAB files###
foreach $file (@files) {
##Open the .TAB file for reading
open(TAB_FILE, $inbox.$file) || die "Can't open file: $!";

#create file name
$verifile = $outbox.$file."_FIXED.TAB";

#print column headings in output file
open(OUTPUT_FILE, ">>$verifile")||die "Can't open file: $!";
print OUTPUT_FILE "Column1 Column2 Column3\n";


##Loop through each line of the file (1 part number per line)
while (<TAB_FILE>){
##Use chomp to get rid of carriage returns and spaces
chomp;
($column1, $column2, $column3) = split("\t");
print OUTPUT_FILE $column1."\t".$column2."\t".$column3."\n";


close TAB_FILE;
close OUTPUT_FILE;


}# end for each file
  • Comment on Re^9: How to create an output file for each input file