in reply to Re: Renaming files
in thread Renaming files
Of course, you need to consider what happens if you have a conflict such as what would happen if you had 2 files called 'test file' & 'test_file' - how would you handle this?If it were me, I would build a hash of arrays, the key of the hash being the new file name, and the array elements being all files that would have that new name. Here's a quicky:
#!perl -w use strict; opendir(DIR,".") or die "Couldn't opendir on .: $!"; while($file = readdir(DIR)){ my $oldfile = $file; $file =~ s/\s+/_/g; push @{$hash{$file}}, $oldfile; } closedir(DIR) or warn "Couldn't close directory .: $!"; foreach my $key (keys %hash){ if(scalar(@{$hash{$key}}) != 1){ print "The following files would have been renamed to the same + thing:\n" local $"="\n"; print "@{$hash{$key}}", "\n"; } else{ print "$hash{$key}[0] -> $key " my $status = rename $hash{$key}[0], $key; if(!$status){ print "Failed\n"; } else{ print "Succeeded\n"; } } }
thor
|
|---|