skyler has asked for the wisdom of the Perl Monks concerning the following question:

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"; } }

Replies are listed 'Best First'.
Re: Duplicate Number in a file
by duff (Parson) on Feb 24, 2004 at 20:58 UTC

    Random note: you might want to look at the File::Spec module instead of dealing with path separators directly. Also, the File::Basename module might come in handy.

    WRT your problem, couldn't you just add a couple of lines to your process_dir() routine that checks to see if the file is there and if so, slightly modifies the output filename? Something like this (untested):

    use File::Basename; use File::Spec::Functions; # ... while (my $file = readdir DIR) { # ... # $newfile contains the full path to the new file my $extra = "a"; while (-e $newfile) { my ($path,$basename,$suffix) = fileparse($newfile,'.rtf'); $basename .= $extra; $extra++; $newfile = catfile($path,$basename,$suffix); } copy($oldfile,$newfile) or die; }