What you need to do is rename the files and dirs by order of depth reverse.
What I mean is.. if you have..
dir/ dir/file dir/dir/file
And you rename dir/ to reference_dir/, now you can't rename the other files anymore, because they no longer reside in dir/ but in reference_dir/
I tested this out .. needs more testing to be safe.
#!/usr/bin/perl use strict; use vars qw($VERSION @PATHS); use Getopt::Std::Strict 'dhvp:a:t'; use Cwd; use Carp; use LEOCHARRE::DEBUG; use File::PathInfo::Ext; $VERSION = sprintf "%d.%02d", q$Revision: 1.1.1.1 $ =~ /(\d+)/g; init(); PATH: for my $abs (@PATHS){ my $f = File::PathInfo::Ext->new($abs) or die; # WHAT IS THE NEW FILENAME my $new_filename; $new_filename .= $opt_p if $opt_p; $new_filename .= $f->filename_only; $new_filename .= $opt_a if $opt_a; $new_filename .= '.'. $f->ext if $f->ext; debug("new filename : $new_filename"); # IS IT THE SAME AS OLD ONE if( $new_filename eq $f->filename ){ warn("same"); next PATH; } my $newpath = $f->abs_loc .'/'.$new_filename; debug("old: $abs, new $newpath"); $opt_t and next PATH; my $newloc = $f->rename( $new_filename ); debug("renamed to $newloc"); } exit; sub usage { qq{$0 - prepend or append filenames OPTIONS -d debug on -h help -v version and exit -p string prepend -a string append, before ext -t test only, don't do it AUTHOR Leo Charre leocharre at cpan dot org SEE ALSO }} sub init { $opt_t and $opt_d = 1; $::DEBUG = 1 if $opt_d; $opt_h and print usage() and exit; $opt_v and print $VERSION and exit; my $abs_path= Cwd::abs_path($_[0]) or die('missing arg or cant reso +lve'); debug("base path: $abs_path"); @PATHS = reverse split( /\n/, `find '$abs_path'` ); @PATHS and scalar @PATHS or die("nothing here"); }
In reply to Re: How to prefix a string to all the files in a directories as well subdirectories
by leocharre
in thread How to prefix a string to all the files in a directories as well subdirectories
by perladdict
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |