#! perl.exe -w # Perl Script to change format of .csv files from version 17 to versio +n 24. # This script will add two lines to the beginning of each file #and copy the file to a final directory destination. use File::Copy; $indir = "c:/public/conversion"; $outdir = "c:/public/final"; opendir DH,$indir or die "Cannot open $indir: $!"; ## has all the files and directories in the given ## directory. You'll want to screen it to make sure you're ## opening a file. foreach $file(readdir DH) { $name = $file; next if $name =~ /^\./; # skip over dot files &addheader($_); } close DH; # &changename($_); sub addheader { open FH, "< $indir/$file" or printf "can't open %s\n",$file; $line = <FH>; $_ = $line; close FH; $field1 = m/^Clock/; # Check for Clock at beginning of file printf "\n$file"; if ( $field1 == "1"){ printf "\tv17\tFile Needs Appending\n"; open FH1, "< $indir/$file" or printf "can't open %s\n", $file; + # Open source file open FH2, '> c:/public/final/tmp.csv' or die "can't append: $! +" ; # Open target file - create if not there already print FH2 "PREPEND LINE1\n"; # Prepend text print FH2 "PREPEND LINE2\n"; my @lines = <FH1>; foreach $line ( @lines ) { print FH2 $line; } close FH1; close FH2; #$file = $correct; $oldfile = 'c:/public/final/tmp.csv'; $newfile = "$outdir/$file"; rename $oldfile,$newfile or die"can't rename files\n"; } else { printf "\tv24\tFile Does Not Need Appending\n"; copy "$indir/$file", "$outdir/$file"; } print "\n"; @test = split /_/, $file; $correct ="$test[0]"."\_Events_2003080\n"; $oldfile = 'c:/public/final/$file'; $newfile = "$outdir/$correct"; copy $oldfile, $newfile or die "Can't rename file second time\ +n"; return ; }
In reply to File prepend , copy, & rename by mgolini
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |