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, 01:59 PM
Rich's Avatar
Rich Rich is offline
Contributing User
ASP Free Newbie (0 - 499 posts)
 
Join Date: Sep 2003
Location: Fort Lauderdale, FL
Posts: 152 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 49 m 15 sec
Reputation Power: 22
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





 Free IT White Papers!
 
How to Present Effectively Online
This white paper offers practical and actionable advice on the key steps that any presenter should consider as they plan and execute a Webinar or online meeting.

 
Open Source Security Myths
Open Source Software (OSS) is computer software whose source code is available to the general public with relaxed or non-existent intellectual property restrictions (or arrangement such as the public domain), and is usually developed with the input of many contributors.

 
Power and Cooling Capacity Management for Data Centers
This paper describes the principles for achieving power and cooling capacity management.

 
Scalable, Fault-Tolerant NAS for Oracle - The Next Generation
For several years NAS has been evolving as a storage alternative for Oracle databases, and for good reason: NAS is quite often the simplest, most cost-effective storage approach for Oracle. Learn about the benefits that HP's approach to scalable NAS brings to Oracle environments in this comprehensive white paper.

 
Understanding Web Application Security Challenges
This white paper discusses many common threats and preventive measures for Web application security, and explains what you can do to help protect your organization.

 

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





© 2003-2009 by Developer Shed. All rights reserved. DS Cluster 4 Hosted by Hostway
For more Enterprise Application Development news, visit eWeek