in reply to Simple Perl file rename
(Notice that about 90% of that is comments and usage.)#!/usr/bin/perl # -w switch is off bc HERE docs cause erroneous messages to be display +ed under Cygwin #From the Perl Cookbook, Ch. 9.9 # rename - Larry's filename fixer $help = <<EOF; Usage: rename expr [files] This script's first argument is Perl code that alters the filename (st +ored in \$_ ) to reflect how you want the file renamed. It can do thi +s because it uses an eval to do the hard work. It also skips rename c +alls when the filename is untouched. This lets you simply use wildcar +ds like rename EXPR * instead of making long lists of filenames. Here are five examples of calling the rename program from your shell: % rename 's/\.orig$//' *.orig % rename 'tr/A-Z/a-z/ unless /^Make/' * % rename '$_ .= ".bad"' *.f % rename 'print "$_: "; s/foo/bar/ if <STDIN> =~ /^y/i' * % find /tmp -name '*~' -print | rename 's/^(.+)~$/.#$1/' The first shell command removes a trailing ".orig" from each filename. The second converts uppercase to lowercase. Because a translation is u +sed rather than the lc function, this conversion won't be locale-awar +e. To fix that, you'd have to write: % rename 'use locale; $_ = lc($_) unless /^Make/' * The third appends ".bad" to each Fortran file ending in ".f", somethin +g a lot of us have wanted to do for a long time. The fourth prompts the user for the change. Each file's name is printe +d to standard output and a response is read from standard input. If t +he user types something starting with a "y" or "Y", any "foo" in the +filename is changed to "bar". The fifth uses find to locate files in /tmp that end with a tilde. It +renames these so that instead of ending with a tilde, they start with + a dot and a pound sign. In effect, this switches between two common +conventions for backup files EOF $op = shift or die $help; chomp(@ARGV = <STDIN>) unless @ARGV; for (@ARGV) { $was = $_; eval $op; die $@ if $@; rename($was,$_) unless $was eq $_; }
Using that script, you can say:
rename 's/ABC //' *.docto rename all files with 'ABC ' in the name.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Simple Perl file rename
by Anonymous Monk on Jul 28, 2008 at 17:46 UTC | |
by wnpaul (Initiate) on Aug 03, 2008 at 05:38 UTC | |
by toolic (Bishop) on Jul 28, 2008 at 18:31 UTC | |
by Anonymous Monk on Jul 28, 2008 at 21:09 UTC | |
by toolic (Bishop) on Jul 29, 2008 at 02:05 UTC | |
by clearwater (Initiate) on Jul 29, 2008 at 09:41 UTC | |
by Anonymous Monk on Jul 28, 2008 at 21:12 UTC |