in reply to rename subdirectories
This is adapted from a script of mine that browses a directory recursively and does something else to each file but it should work for you (or should be close enough - I have not tested it). The filename is made of the path to it plus its name. This name may identify a file or a directory. If it is a directory go in and do your job there first by renaming the files and then you rename the directory itself as well. If you don't follow this order, you'll end up trying to browse a directory that no longer exists (because you've read it's name but you've also renamed it before you tried to browse it).sub rename($) { my $path = $_[0]; #append a trailing / if it's not there if($path !~ /\/$/) { $path .= '/'; } #loop through the files contained in the directory for my $eachFile (glob($path.'*')) { #if the file is a directory if(-d $eachFile) { #pass the directory to the routine ( recursion ) rename($eachFile); } else { my @strings = split('/', $eachFile); my $fileName = $strings[$#strings]; my $filePath = '/'.join('/', @strings[2..$#strings-1]); system("mv $filePath/$fileName $filePath/\"YourNewNameHere\""); } } }#rename
That sample system call is one way of doing it and assumes you're using unix (or cygwin on windows). You can definitely change that or maybe use a Perl package for file handling if the OS is a problem.
Hope this helps!
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: rename subdirectories
by andreas1234567 (Vicar) on Dec 04, 2007 at 13:31 UTC |