UNIX - Renaming files Multiple Files 
Author Message
 UNIX - Renaming files Multiple Files

I'm trying to find away to rename multiple files on a Unix Solaris 8
Platform.  I've been told this can be done using PERL

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  



Sun, 02 Jan 2005 10:26:59 GMT  
 UNIX - Renaming files Multiple Files
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,

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
  {



  opendir(DIR,$path);

  closedir(DIR);

  $ct = 0;

    {
    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);


  $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

  }

####################


I'm trying to find away to rename multiple files on a Unix Solaris 8
Platform.  I've been told this can be done using PERL

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



Sun, 02 Jan 2005 10:39:29 GMT  
 UNIX - Renaming files Multiple Files

Quote:
> I'm trying to find away to rename multiple files on a Unix Solaris 8
> Platform.  I've been told this can be done using PERL

> All files have this format.

>  Ex:  T0400-ptu.txt    ptf0400-ptu.txt

>  There are approximately 300 files need renamed.

>   Any Help would be appreciated.

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

HTH
Adam

#!/usr/bin/perl -w
use strict ;
my ( $newfilename, $oldfilename ) ;


    $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") ;
    }

Quote:
}



Sun, 02 Jan 2005 14:36:08 GMT  
 UNIX - Renaming files Multiple Files
[Please don't top-post]

Quote:

> 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.

Quote:
> ####################
> #!/usr/bin/perl

No warnings.  No strict.

Quote:
> # 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?

Quote:
> 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?

Quote:
> $path = $ARGV[0];
> $outputf = $ARGV[1];
> $get_subs = $ARGV[2];

> open(OUT, ">".$outputf); ##Open Output

What happens when open fails to open the file?

Quote:
> getfiles($path, $get_subs);
> close(OUT);

> exit;

> ###Sub Routines

> sub getfiles

Why not use File::Find?

Quote:
>   {




Why are you using an array slice when you should be using a scalar?

Quote:
>   opendir(DIR,$path);

What happens when opendir fails to open the directory?

Quote:

>   closedir(DIR);

>   $ct = 0;

>     {
>     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?

Quote:
>     $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?

- Show quoted text -

Quote:
>       {
>       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);



Why are you using an array slice when you should be using a scalar?

Quote:
>   $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?

Quote:
>  #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?

- Show quoted text -

Quote:
>    { 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


Sun, 02 Jan 2005 15:00:46 GMT  
 UNIX - Renaming files Multiple Files
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1


Quote:
> I'm trying to find away to rename multiple files on a Unix Solaris 8
> Platform.  I've been told this can be done using PERL

> All files have this format.

>  Ex:  T0400-ptu.txt    ptf0400-ptu.txt

>  There are approximately 300 files need renamed.

>   Any Help would be appreciated.

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-----



Sun, 02 Jan 2005 18:41:51 GMT  
 UNIX - Renaming files Multiple Files

Quote:
> I'm trying to find away to rename multiple files on a Unix Solaris
> 8 Platform.  I've been told this can be done using PERL
> 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



Sun, 02 Jan 2005 20:32:26 GMT  
 UNIX - Renaming files Multiple Files

Quote:
> I'm trying to find away to rename multiple files on a Unix Solaris 8
> Platform.  I've been told this can be done using PERL

It sure can be done. See "perldoc -f rename"

Quote:
> All files have this format.
> Ex:  T0400-ptu.txt    ptf0400-ptu.txt

Ok...
And what is the desired target file name format?

Quote:
> There are approximately 300 files need renamed.

No problem, just "use File::Find;"

jue



Mon, 03 Jan 2005 01:04:46 GMT  
 
 [ 7 post ] 

 Relevant Pages 

1. UNIX - Renaming files Multiple Files

2. Renaming multiple files from the command line ??

3. is it possible to rename multiple files?

4. Using rename script to change multiple files

5. Rename multiple files using cgi

6. Help with multiple text files, search and replace, rename

7. renaming files w/wildcards console unix

8. renaming file problem (rename / mv)

9. File rename deletes files

10. Finding text in a file and renaming that file

11. Reading multiple files and saving to one file

12. Multiple edits, multiple files

 

 
Powered by phpBB® Forum Software