Brothers - I seek your guideance. I am developing (on a novice level mind you) a script to: 1. Look at files(csv) in a directory 2. Check for version. 3. Copy version "24" directly to an output directory. 4. Prepend two lines to version "17" and copy it to the same directory as version 24. 5. Change all the file names (all files are now v24) to read "something_Events_date" I can get up to step 4 with no big problems, but Step 5 is giving me fits. I split on an underscore and save it in an array. But renaming the file a second time does not work well. If someone can point out what I am doing incorrectly, I would appreciate it.
#! 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

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.