I need to recursively rename every file and directory. I convert spaces to underscores and make all file/directory names to lowercase. How can I make the following script rename all files in one run? Currently the script needs to be run several times before all the files/directories are converted. The code is below:
#!/usr/bin/perl
use File::Find;
$input_file_dir = $ARGV[0];
sub process_file {
$clean_name=lc($_);
$clean_name=~s/\s/_/g;
rename($_,$clean_name);
print "file/dir name: $clean_name\n";
}
find(\&process_file, $input_file_dir);
You either need to specify bydepth => 1
in the options you pass to find or call finddepth
. From perldoc File::Find:
bydepth
Reports the name of a directory only AFTER all its entries have been reported. Entry point
finddepth()
is a shortcut for specifying{ bydepth => 1 }
in the first argument offind()
.
However, you still need to decide how to deal with naming clashes because rename will clobber the target if the target exists.
#!/usr/bin/perl
use strict; use warnings;
use File::Find;
finddepth(\&process_file, $_) for @ARGV;
if you are open to other approaches, here's a Python solution
import os
for R,DIR,FILES in os.walk("/mypath",topdown=False):
for file in FILES:
newfile=file.lower().replace(" ","_")
new_file_name=os.path.join(R,newfile)
os.rename( os.path.join(R,file) , new_file_name)
for dir in DIR:
newdir=dir.lower().replace(" ","_")
new_dir_name=os.path.join(R,newdir)
os.rename( os.path.join(R,dir) , new_dir_name)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With