All files have this format.
Ex: T0400-ptu.txt ptf0400-ptu.txt
There are approximately 300 files need renamed.
Any Help would be appreciated.
Jess
Enjoy,
Joe
####################
#!/usr/bin/perl
# case.pl
#
#
# This script strips opens a directory and replaces
# all caps files/dir w/lowercase file/dirs
#
# Joe
#
# 1.0 -> 12DEC01
# BORN
system("clear");
if($ARGV[0] eq "" || $ARGV[0] eq "--help" || $ARGV[0] eq "?" || $ARGV[0] eq
"-h")
{ usage(); exit; }
if($ARGV[1] eq "" || $ARGV[1] eq "--help" || $ARGV[1] eq "?" || $ARGV[1] eq
"-h")
{ usage(); exit; }
if($ARGV[2] eq "" || $ARGV[2] eq "--help" || $ARGV[2] eq "?" || $ARGV[2] eq
"-h")
{ usage(); exit; }
$path = $ARGV[0];
$outputf = $ARGV[1];
$get_subs = $ARGV[2];
open(OUT, ">".$outputf); ##Open Output
getfiles($path, $get_subs);
close(OUT);
exit;
###Sub Routines
sub getfiles
{
my ($path, @files, $fname, $get_subs);
$path = @_[0];
$get_subs = @_[1];
opendir(DIR,$path);
@files = readdir(DIR);
closedir(DIR);
$ct = 0;
foreach $fname (@files)
{
my ($full_path, $c_full_path, $p_full_path, $dir_full_path);
$full_path = sprintf("%s%s", $path, $fname);
$c_full_path .= $path . ".";
$p_full_path .= $path . "..";
# print "\n\nSTART:\nFULL PATH: $full_path\n";
# print "\nC_FULL PATH: $c_full_path\n";
# print "P_FULL PATH: $p_full_path\n";
# print "\nOriginal Path: $path\nOrig Fname: $fname\n";
# print "New Path: $path\nNew Fname: $new_fname\n\n";
if($full_path eq $c_full_path) { print "Skip \"CUR:$full_path\"\n";
next; } # skip cur dir
if($full_path eq $p_full_path) { print "Skip \"PAR:$full_path\"\n";
next; } # skip par dir
($new_path, $new_fname) = &changename($path, $fname, $outputf);
if($get_subs eq "1")
{
if(-d $full_path) # dir find
{
$dir_full_path .= $full_path . "/";
# print "\nDIRECTORY: $full_path\n";
# print "DIREC: $dir_full_path\n";
getfiles($dir_full_path);
next;
}
}
}
}
sub changename
{
my ($path, $fname, $new_path, $new_fname, $full_path, $new_full_path);
my ($full_path, $c_full_path, $p_full_path);
$path = @_[0];
$fname = @_[1];
$full_path = sprintf("%s%s", $path, $fname);
$new_fname = lc($fname);
$new_full_path = sprintf("%s%s", $path, $new_fname);
#print "\n\nSTART:\nFULL PATH: $full_path\n";
#print "\nC_FULL PATH: $c_full_path\n";
#print "P_FULL PATH: $p_full_path\n";
#print "\nOriginal Path: $path\nOrig Fname: $fname\n";
#print "New Path: $path\nNew Fname: $new_fname\n\n";
rename($full_path, $new_full_path);
{ print OUT "C: \"$full_path\" -> \"$new_full_path\"\n"; }
}
sub usage
{
print << 'EOT';
SYNTAX: perl case.pl path outputf subs
USAGE: Take a directory/sub dir and convert all caps/single to lowercase
Notes:
1. files/subs either "0" or "1"
-0 = current files in direc
-1 = all current file and file in subdirs
ex "./case.pl /home/joe out 1"
EOT
}
####################
"Jess Walton" <jessw...@hotmail.com> wrote in message
news:20020716...@localhost.localdomain...
I'm not quite sure what it is you want. Do you want to rename
Txxx.xxx to ptfxxx.xxx?
IAC, you might benefit from this classic Larry script:
#!/usr/bin/perl -w
# rename - Larry's filename fixer
$op = shift or die "Usage: rename expr [files]\n";
chomp(@ARGV = <STDIN>) unless @ARGV;
for (@ARGV) {
$was = $_;
eval $op;
die $@ if $@;
rename($was,$_) unless $was eq $_;
}
With this you can do:
$ rename 's/^T/ptf/;' *-ptu.txt
This example works similarly to (MS|PC)-DOS' RENAME command
..ooOO( C:\> rename *.doc *.txt )
...but Larry's rename(1) is much more powerful, since you can insert any
perl expression or command in place of the substitution.
HTH
Tim Hammerquist
--
And now for something completely different.
Ok, whith KornShell...
for f in T*-ptu.txt; do echo mv $f ptf${f#T}; done
Assuming you want to rename all files called
T<something>-ptu.txt to ptf<something>-ptu.txt
Remove the "echo" to do it for real (otherwise it will only
print out the command that would get executed).
--
Andreas Kähäri
--------------------------------------------------------------
Stable, secure, clean, free: www.netbsd.org
Here's one I wrote recently. I just copy it and modify the critical line
when I need to use it -- this should do what you want. Give it * or *.txt
as its argument and the shell will glob and pass all 300 filenames to the
Perl program in @ARGV.
HTH
Adam
#!/usr/bin/perl -w
use strict ;
my ( $newfilename, $oldfilename ) ;
foreach $oldfilename (@ARGV) {
$newfilename = $oldfilename ;
$newfilename =~ s/^T/ptf/ ;
if ( $newfilename eq $oldfilename) {
print ("$oldfilename -- unchanged\n" );
}
elsif (-e $newfilename) {
print ("$oldfilename -- ignored -- $newfilename exists\n") ;
}
else {
rename ($oldfilename, $newfilename) ;
print ("$oldfilename --> $newfilename\n") ;
}
}
Joe Snuffy wrote:
>
> Hello... I wrote a script that rename all files in a directory to lower
> case..
> you can hack the script and use what you need...
>
> Enjoy,
I can't, it hurts me just to look at it.
> ####################
> #!/usr/bin/perl
No warnings. No strict.
> # case.pl
> #
> #
> # This script strips opens a directory and replaces
> # all caps files/dir w/lowercase file/dirs
> #
> # Joe
> #
> # 1.0 -> 12DEC01
> # BORN
>
> system("clear");
Why clear the screen for a command line program?
> if($ARGV[0] eq "" || $ARGV[0] eq "--help" || $ARGV[0] eq "?" || $ARGV[0] eq
> "-h")
> { usage(); exit; }
> if($ARGV[1] eq "" || $ARGV[1] eq "--help" || $ARGV[1] eq "?" || $ARGV[1] eq
> "-h")
> { usage(); exit; }
> if($ARGV[2] eq "" || $ARGV[2] eq "--help" || $ARGV[2] eq "?" || $ARGV[2] eq
> "-h")
> { usage(); exit; }
Why not use Getopt::Std or Getopt::Long?
> $path = $ARGV[0];
> $outputf = $ARGV[1];
> $get_subs = $ARGV[2];
>
> open(OUT, ">".$outputf); ##Open Output
What happens when open fails to open the file?
> getfiles($path, $get_subs);
> close(OUT);
>
> exit;
>
> ###Sub Routines
>
> sub getfiles
Why not use File::Find?
> {
> my ($path, @files, $fname, $get_subs);
>
> $path = @_[0];
> $get_subs = @_[1];
Why are you using an array slice when you should be using a scalar?
> opendir(DIR,$path);
What happens when opendir fails to open the directory?
> @files = readdir(DIR);
> closedir(DIR);
>
> $ct = 0;
> foreach $fname (@files)
> {
> my ($full_path, $c_full_path, $p_full_path, $dir_full_path);
> $full_path = sprintf("%s%s", $path, $fname);
Why use sprintf when "$path$fname" or $path . $fname does the same
thing?
> $c_full_path .= $path . ".";
> $p_full_path .= $path . "..";
>
> # print "\n\nSTART:\nFULL PATH: $full_path\n";
> # print "\nC_FULL PATH: $c_full_path\n";
> # print "P_FULL PATH: $p_full_path\n";
> # print "\nOriginal Path: $path\nOrig Fname: $fname\n";
> # print "New Path: $path\nNew Fname: $new_fname\n\n";
>
> if($full_path eq $c_full_path) { print "Skip \"CUR:$full_path\"\n";
> next; } # skip cur dir
> if($full_path eq $p_full_path) { print "Skip \"PAR:$full_path\"\n";
> next; } # skip par dir
>
> ($new_path, $new_fname) = &changename($path, $fname, $outputf);
>
> if($get_subs eq "1")
Why use the string-wise equality test on a numeric value?
> {
> if(-d $full_path) # dir find
> {
> $dir_full_path .= $full_path . "/";
> # print "\nDIRECTORY: $full_path\n";
> # print "DIREC: $dir_full_path\n";
> getfiles($dir_full_path);
> next;
> }
> }
> }
> }
>
> sub changename
> {
> my ($path, $fname, $new_path, $new_fname, $full_path, $new_full_path);
> my ($full_path, $c_full_path, $p_full_path);
> $path = @_[0];
> $fname = @_[1];
Why are you using an array slice when you should be using a scalar?
> $full_path = sprintf("%s%s", $path, $fname);
> $new_fname = lc($fname);
> $new_full_path = sprintf("%s%s", $path, $new_fname);
Why use sprintf when "$path$fname" or $path . $fname does the same
thing?
> #print "\n\nSTART:\nFULL PATH: $full_path\n";
> #print "\nC_FULL PATH: $c_full_path\n";
> #print "P_FULL PATH: $p_full_path\n";
> #print "\nOriginal Path: $path\nOrig Fname: $fname\n";
> #print "New Path: $path\nNew Fname: $new_fname\n\n";
>
> rename($full_path, $new_full_path);
What happens when rename fails to rename the file?
> { print OUT "C: \"$full_path\" -> \"$new_full_path\"\n"; }
> }
>
> sub usage
> {
> print << 'EOT';
>
> SYNTAX: perl case.pl path outputf subs
>
> USAGE: Take a directory/sub dir and convert all caps/single to lowercase
>
> Notes:
> 1. files/subs either "0" or "1"
> -0 = current files in direc
> -1 = all current file and file in subdirs
>
> ex "./case.pl /home/joe out 1"
>
> EOT
> }
John
--
use Perl;
program
fulfillment
Jess Walton <jessw...@hotmail.com> wrote in news:20020716.22264600
@localhost.localdomain:
I have two scripts which I wrote that I've used for years to do this.
One is called 'cppat' (for copying files according to a pattern), one is
called 'mvpat' (for moving). You can find them at:
http://employeeweb.myxa.com/eric/perl/cppat
http://employeeweb.myxa.com/eric/perl/mvpat
To use them, you supply an arbitrary Perl expression and a list of files:
cppat 's/pl$/bak/' *.pl # copies *.pl to *.bak
mvpat '$_ = lc $_' * # renames all files to lowercase
The filename is given to your expression in $_; you then mangle $_ to be
whatever you want the new name to be.
The scripts take three options:
-n "not": display the copy/move results but don't actually do it.
-q "quiet": don't display the names of the files as they're done.
-s "safe": don't overwrite existing files.
Hope you find this useful.
- --
Eric
print scalar reverse sort qw p ekca lre reh
ts uJ p, $/.r, map $_.$", qw e p h tona e;
-----BEGIN PGP SIGNATURE-----
Version: PGPfreeware 7.0.3 for non-commercial use <http://www.pgp.com>
iQA/AwUBPTVLKWPeouIeTNHoEQJzkACfXsjw1sXMYJiKDjhnF17PQCsVbv0AoJX1
adIwuIz9m6Gpdhlz78yzAfiA
=RGoI
-----END PGP SIGNATURE-----
> All files have this format.
> Ex: T0400-ptu.txt ptf0400-ptu.txt
As a one-liner:
perl -e 'while(<*.txt>){$n=$_;$n=~s/^T(.+)$/ptf$1/;rename $_,$n;}'
Change *.txt to something more unique if necessary.
Note that rename will blindly overwrite the target file if it exists and if you
have write permissions to it.
Petri Oksanen
> Ok, whith KornShell...
>
Are you aware this is a Perl newsgroup?
It sure can be done. See "perldoc -f rename"
> All files have this format.
> Ex: T0400-ptu.txt ptf0400-ptu.txt
Ok...
And what is the desired target file name format?
> There are approximately 300 files need renamed.
No problem, just "use File::Find;"
jue
Yes I am.
Some thing are best done in Perl, some others are not.
But in a Perl newsgroup, the best things done are Perl ;)