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

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.