$File::Find::dir is the current directory name, $_ is the current filename within that directory $File::Find::name is the complete pathname to the file.So, you don't need File::Basename to parse the file and directory. Unless your files are on a different drive, you could safely give the path without the drive letter, C:.
marinersk pointed out the problem with your approach. A correct solution might be stated (below) using the 3 variables that come with File::Find.
Also notice that perl can read forward slashes on a Windows machine - it's not necessary to have backslashes.
This will rename all the files in folder1 and any subdirectories under it. (You might want to print out the proposed name changes before doing the operation to see that all is well.
#!/usr/bin/perl use strict; use warnings; use File::Find; find(\&wanted, '/folder1'); sub wanted { if (-f) { (my $new = $File::Find::name) =~ s/\//./g; print "$File::Find::dir/$new\n"; #rename $File::Find::name, "$File::Find::dir/$new" # or warn "Can't rename $File::Find::name\n"; } }
In reply to Re: rename file name in directory and subdir
by Cristoforo
in thread rename file name in directory and subdir
by starface245
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |