Hello Monks, Sometime ago I have written this code to read a file and look for a file base on a path. Then It would copy the file to another directory and rename it with a number found at begin of line. It works fine however I didn't notice that the file starts coming with duplicate numbers. They have different paths but since I rename the file with begin of line number. It takes the file and renames it the same and doesn't copy to the destination directory. For Example:
123456|H:\00005199.016 123456|H:\00001598.017 Files get rename to 123456.rtf and 123456.rtf (They are completly different files) Unfortunately it +takes the content of the previous one which it is a problem.
I haven't used hash, do you have any ideas to have it check to previous numbers in a hash array if it finds a duplicate to rename the duplicate with 123456.rtf, 123456a.rtf and so forth. Any suggestions would be appreciated.
#! perl -w use strict; use File::Copy; my $cnt; my $infile = "C:\\(Directory)\\doclist2.chr"; my ( $yr, $mo, $dy ) = (localtime)[5,4,3]; my $outfile = sprintf( "C:\\(Directory)\\%04d%02d%02d.txt",$yr+1900,$m +o+1,$dy ); my $staticdir = "C:\\(Directory)\\process\\"; open IN, "<$infile" or die "Couldn't open $infile, $!"; open OUT,">$outfile" or die "Couldn't open $outfile, $!"; $cnt++; while (<IN>) { chomp; my @fields = split /\|/; my $newfile = $fields[0]; my $path_str = $fields[20]; do { warn "Empty field 19"; next } unless $path_str; my @path = split /\\/, $path_str; my $dir = join "\\", @path[ 0, 1, 2, 3, 4, 5, 6 ]; $newfile =~ s/$/.rtf/; my $out = join ('|', @fields[0..19]) . "@@" . $staticdir . $newfil +e; print OUT "$out\n"; process_dir($dir,$newfile); } close IN; sub process_dir { my ($dir, $newfile) = @_; do { warn "$dir does not exist!\n"; return } unless -e $dir; opendir DIR, $dir or do { warn "Could not open $dir $!\n" ; return }; while ( my $file = readdir DIR ) { print "dir: $dir file:$file newfile:$newfile\n"; #before the next unless statements. next unless -f "$dir\\$file"; next unless $file =~ m/\.rtf$/i; copy( "$dir\\$file", "C:\\(Directory)\\process\\$newfile" ) or die "Failed to copy $file: $!\n"; } }

In reply to Duplicate Number in a file by skyler

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.