Code Bank
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
 
 
User Name:
Password:
Remember me
Go Back   ASP Free ForumsProgrammingCode Bank

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread ASP Free Forums Sponsor:
  #1  
Old February 6th, 2004, 12:59 PM
Rich's Avatar
Rich Rich is offline
Administrator
Developer Shed Admin.
 
Join Date: Sep 2003
Location: Fort Lauderdale, FL
Posts: 151 Rich User rank is Sergeant (500 - 2000 Reputation Level)Rich User rank is Sergeant (500 - 2000 Reputation Level)Rich User rank is Sergeant (500 - 2000 Reputation Level)Rich User rank is Sergeant (500 - 2000 Reputation Level)Rich User rank is Sergeant (500 - 2000 Reputation Level) 
Time spent in forums: 11 h 2 m 30 sec
Reputation Power: 10
Rename (move) files, allowing very sophisticated wild-carding

#!/usr/local/bin/perl
#From: schip@lmsc.lockheed.com (Jan Schipmolder)

require 'getopts.pl';
&Getopts ( 'fhist' ) || ( &usage, exit ) ;

&usage, exit if $opt_h;

( $perlexpr = shift ) || ( &usage, exit );

if ( ! @ARGV ) {
@ARGV = <STDIN>;
chop ( @ARGV );
}

$would = "would move" if $opt_t;

foreach ( @ARGV ) {
$old = $_;
eval $perlexpr;
die if $@;
if ( $_ eq $old ) {
printf ( "%s %-15s\n", $would, $old ) unless $opt_s;
} else {
if ( $opt_t ) {
} else {
if ( ! $opt_f and -e $_ ) {
if ( $opt_i ) {
printf ( " %-15s exists, Overwrite? [y/n] ", $_ );
$ans = scalar(<STDIN>);
if ( $ans !~ /^y/i ) {
next;
}
} else {
printf ( " %-15s exists\n", $_ ) unless $opt_s;
next;
}
}
rename ( $old, $_ ) || die "could not rename $old->$_\n";
}
printf ( "%s %-15s-> %s\n", $would, $old, $_ ) unless $opt_s;
}
}

sub error {
print @_[0], "\n";
print "$0 aborted\n";
exit;
}

sub usage {
print <DATA>;
}
__END__

Name:
rename

Purpose:
Rename (move) files, allowing very sophisticated wild-carding

Usage:
rename [ -hst ] 'what2do' [ files ]

where
-h -- (help) -- shows this help, then quits (does not execute)
-f -- (force) -- overwrite existing file without asking
-i -- (interactive) -- asks before overwriting existing file
-s -- (silent) -- suppresses info-lines
-t -- (test) -- shows what would be done, but does not rename
what2do -- an expression stating how to rename the files
files -- a list of zero or more files to be renamed. Rename uses
the standard input if files is absent

Example:
rename -h > a.help
Captures the help infor for rename in a.help

Example:
rename 's/alfa/beta/' *
Each non-dot file in the current directory whose name contains
the string "alfa" is renamed so that the first occurrance of
"alfa" is replaced by "beta". An info-line is printed for each
file considered.

Example:
ls *.f | rename -t 's/x/abc/g'
Each file in the collection of files *.f is renamed if it
contains the letter "x". If it does, each "x" in its name is
replaced by the string "abc". The actual renaming (moving) of
the files is suppressed, so that you first can peek at the
info-lines.

Example:
rename -s 'tr/A-Z/a-z/' * .*
Each of the files in the current directory, if its name contains
any upper case letter, is moved such that all upper case letters
are replaced by lower case letters. Unless an error occurs, no
info lines are printed.

Example:
rename 's/$/.old/' *.a
Each file in the collection of files *.a is moved to a file
whose name equals the original file name plust the extension
".old".

Example:
rename 's/(.*)\.f$/f.$1/' *
Each non-dot file in the current directory, if its name ends
with the extension ".f", is renamed so that its new name begins
with "f." followed by the original prefix of ".f" (e.g.,
anything.f moves to f.anything).

Example:
ls */* | rename 's/^([a-z]{5})(\d{3})$/$2$1/'
Each no-dot file in each of the non-dot subdirectories of the
current directory is considered for renaming. If the name of the
file consists of exactly 5 lower case letters followed by 3
digits, it is renamed so that the letters and digits are
switched: the new name has first the original three digits, and
then the original five letters.

Reply With Quote
Reply

Viewing: ASP Free ForumsProgrammingCode Bank > Rename (move) files, allowing very sophisticated wild-carding


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 1 hosted by Hostway