MrCBofBCinTX
Technical User
I am using this and several other similar functions to rename files and directories, changing spaces to underscores, remove repeated dashes, underscores, and changing to title case.
code varies for each, but all similar.
Basically it all does its job, but I am getting unable to change errors which go away on second or third try.
I have a test set of file and directories I am testing against.
Does anyone see my error here?
Code:
foreach my $file (@change_these) {
next if $file =~ /^\.\.?$/; # skip . and ..
chomp $file;
my $newfile = $file;
if (($newfile =~ s/ /_/g)) {
if (rename($file, $newfile)) {
$count++;
if ($verbose) {
print "$file -> $newfile\n";
}
} else {
warn("\nUnable to change filename:\n$file\n to:\n$newfile\n");
$failed++;
}
}
}
code varies for each, but all similar.
Basically it all does its job, but I am getting unable to change errors which go away on second or third try.
I have a test set of file and directories I am testing against.
Does anyone see my error here?
Code:
foreach my $file (@change_these) {
next if $file =~ /^\.\.?$/; # skip . and ..
chomp $file;
my $newfile = $file;
$newfile = lc $newfile;
$newfile = ucfirst $newfile;
$newfile =~ s/_([a-z])/_\u$1/g; #After underscore
$newfile =~ s/-([a-z])/-\u$1/g; #After dash
$newfile =~ s/ ([a-z])/ \u$1/g; #After space
unless ($newfile eq $file) {
if (rename($file, $newfile)) {
$count++;
if ($verbose) {
print "$file -> $newfile\n";
}
} else {
warn("Unable to change filename $file to $newfile");
$failed++;
}
}
}