Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight
 
PerlMonks  

Re: How to prefix a string to all the files in a directories as well subdirectories

by leocharre (Priest)
on Dec 29, 2008 at 20:52 UTC ( #733116=note: print w/replies, xml ) Need Help??


in reply to How to prefix a string to all the files in a directories as well subdirectories

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"); }
  • Comment on Re: How to prefix a string to all the files in a directories as well subdirectories
  • Download Code

Replies are listed 'Best First'.
Re^2: How to prefix a string to all the files in a directories as well subdirectories
by perladdict (Chaplain) on Dec 30, 2008 at 09:10 UTC
    Hi leocharre, Thanks for your quick reply.
    I am not intended to rename directories as well sub directories, all of these directories and subdirectories stores mobile application screen shot in .bmp files with name first_screen.bmp in all directoris. Only thing i want to prefix a string
    "reference_" to all of these files recursively.Can u help me out how efficianlty i can prefix a string to all these files.Thanks in advance for your reply.
      Like this:
      #!/usr/bin/perl use strict; use warnings; use File::Find; sub callback { my $file = $_; rename $file,"reference_$file" if -f $file } my $dir = 'C:/Image_Repository'; find (\&callback, $dir);
      Of course, there is always More Than One Way To Do It, so you can "hand craft" it if you enjoy lots of typing, but it probably won't be as efficient as using File::Find.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://733116]
help
Chatterbox?
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others chanting in the Monastery: (3)
As of 2017-12-13 02:13 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    What programming language do you hate the most?




















    Results (344 votes). Check out past polls.

    Notices?