| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
#1
|
||||
|
||||
|
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. |
![]() |
| Viewing: ASP Free Forums > Programming > Code Bank > Rename (move) files, allowing very sophisticated wild-carding |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|