Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked
 
PerlMonks  

Recursive Rename

by coolmichael (Deacon)
on Apr 09, 2001 at 11:54 UTC ( #70944=CUFP: print w/replies, xml ) Need Help??

This is a handy little script to traverse a file directory and prompt the user to rename files that match a specific pattern ($re). When prompted, type the new name of the file. The script will give you a confirmation prompt before overwriting or moving any files.

Please give me any comments you wish, preferably some that are related.

#!/usr/bin/perl -w use strict; use File::Find; my $new; my $ans; my $re='~'; my $start='/home/michael'; finddepth(\&wanted, $start); sub wanted { return unless m/$re/; print "$File::Find::name\n-->"; chomp ($new = <>); return if (length $new == 0); if(($new =~ m|^\.\./|) || ($new =~ m|/|)){ $ans="?"; while($ans !~ m/^[y|n]$/i){ print "move? (y|n): "; chomp ($ans =<>); } return if ($ans =~ /n/); } if(-e $new){ $ans="?"; while($ans !~ m/^[y|n]$/i){ print "overwrite? (y|n): "; chomp ($ans =<>); } return if ($ans =~ /n/); } rename ($_, $new); }

michael
the blue haired monk

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: CUFP [id://70944]
Approved by root
help
Chatterbox?
[Corion]: Mhrrrr. Some project made changes to a report I ingest. I tell them "the format of the new rows is bad". They change it, without understanding/ knowing what other systems might act on this suffix. While it now works for me, I'm not really happy with ...
[Corion]: ... their approach to changing things and waiting who screams at them.

How do I use this? | Other CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (11)
As of 2017-07-24 14:04 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    I came, I saw, I ...
























    Results (354 votes). Check out past polls.