Hi,
I'm try to rename a directory recursively. I want to run a script at a directory and rename all subdirectories containning "-" to "_".
Eg: C:\temp
|_C:\temp\test\level-1
|_C:\temp\test\level-1\level-2
I use File::Find, which works find, but I get a problem once the directory has been modified. Here is the error:
Can't cd to (C:\temp\test/) level-1 : No such file or directory
I tried adding the no_chdir, but I didn't see a difference.
This is the code (still very preliminary):
use File::Find;
find (sub { \&process_file(), no_chdir}, "C:\\Temp\\test"
sub process_file {
if (($_ =~ /-/) && (-d $File::Find::name)) {
# Store passed directory name
my $tmpname = $_;
# Replace all "-" by "_"
$tmpname =~ s/-/_/g;
# Concatenate new name
my $newname = "$File::Find::dir/$tmpname";
# change $newname
my $rename = rename($File::Find::name, $newname) or die "Couldn't rename $_ to $newname: $!\n";
}
}
I understand the problem; but I don't know what to do.
Does anyone have an idee?
Thanks,
Thierry
I'm try to rename a directory recursively. I want to run a script at a directory and rename all subdirectories containning "-" to "_".
Eg: C:\temp
|_C:\temp\test\level-1
|_C:\temp\test\level-1\level-2
I use File::Find, which works find, but I get a problem once the directory has been modified. Here is the error:
Can't cd to (C:\temp\test/) level-1 : No such file or directory
I tried adding the no_chdir, but I didn't see a difference.
This is the code (still very preliminary):
use File::Find;
find (sub { \&process_file(), no_chdir}, "C:\\Temp\\test"
sub process_file {
if (($_ =~ /-/) && (-d $File::Find::name)) {
# Store passed directory name
my $tmpname = $_;
# Replace all "-" by "_"
$tmpname =~ s/-/_/g;
# Concatenate new name
my $newname = "$File::Find::dir/$tmpname";
# change $newname
my $rename = rename($File::Find::name, $newname) or die "Couldn't rename $_ to $newname: $!\n";
}
}
I understand the problem; but I don't know what to do.
Does anyone have an idee?
Thanks,
Thierry