Contributed by Anonymous Monk
on Jun 21, 2000 at 20:15 UTC
Q&A
> files
Description: I want to move xxx_yyy.zzz to xxx-yyy.zzz, but cant figure
out how to automate this. Any ideas? Answer: How do I do a bulk rename? contributed by jjhorner Try this:
#!/usr/bin/perl -w
foreach (@ARGV){
$name = $_ ;
$name =~ s/_/-/;
rename($_, $name);
print "$_ renamed to $name\n";
}
usage: renamer.pl *.zzz
J. J. Horner
| Answer: How do I do a bulk rename? contributed by Shendal Use File::Copy.
use File::Copy;
use strict;
my($sourcedir) = shift || die "No source dir";
opendir(DIR,"$sourcedir");
foreach (grep !/^\.\.?$/,readdir DIR) {
if (/^(\S+)_(\S+)\.(\S+)$/) {
if (move($_,"$1-$2.$3")) {
print "Moved $_ to $1-$2.$3\n";
} else {
print "Error moving $_ to $1-$2.$3\n";
}
}
}
HTH,
Shendal
| Answer: How do I do a bulk rename? contributed by kryberg #!/usr/bin/perl -w
# rename all files with a certain extention in a directory
use strict;
my $dirname;
$dirname = '/substitue_your_path_here';
opendir(DIR, $dirname) or die "Can't opendir $dirname: $!";
while ( defined (my $file = readdir DIR) ) {
# ignore current and parent directory . and ..
next if $file =~ /^\.\.?$/;
my $new = $file;
# substitute new file extension
$new =~ s/\.old_extension$/\.new_extension/;
rename($file,$new)
}
Reference Perl Cookbook Chapter 9 and Programming Perl Chapter 29 | Answer: How do I do a bulk rename? contributed by fundflow You can use Rename files safely which will make sure that no files are erased in the process.
|
Please (register and) log in if you wish to add an answer
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, 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, 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: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.
|
Log In?
|
|
Chatterbox?
|
How do I use this? | Other CB clients
|
Other Users?
|
Others musing on the Monastery: (2) As of 2021-03-01 05:14 GMT
|
Sections?
|
|
Information?
|
|
Find Nodes?
|
|
Leftovers?
|
|
Voting Booth?
|
No recent polls found
|
Notices?
|
|
|