skyler has asked for the wisdom of the Perl Monks concerning the following question:
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.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.
#! 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 |