Joes has asked for the wisdom of the Perl Monks concerning the following question:

Hi, this is my effort at renaming .puz files, and transferring to a sub directory.

I have plagiarised tachyon's snippet for renaming files
My modification does not work, and I would appreciate any comments and assistance.

I have left the debugging print statement in, in case anyone wants to set up a dummy folder with some .puz files and an empty subdirectory

Many thanks,
Joe, Townsville

#!/usr/bin/perl -w use strict; my $dir = "c:/Xwords/"; my $n=length "c:/Xwords/"; # There must be a better way of doing t +his => Please?? my $from = '.puz'; my $to = 'A.puz'; my $newdir = "c:/Xwords/PrintedXwords/"; while (<$dir*$from>) { # checking for .puz files my ($old,$new,$full); # declaring variables $old = $full = $_; # setting the full filename and path $new = $old = substr $_,$n; # removing the path details of + filename $new =~ s/$from$/$to/; # changing the old file name print " \n \n"; print " full name of original file is $full \n"; print " name of original file is $old \n"; print " full name of new file is {$newdir.$new} \n"; # does not co +ncatenate - leaves the '.' visible print " \n \n"; rename $full, {$newdir.$new}; } print "Files renamed !"; exit

Replies are listed 'Best First'.
Re: Renaming and moving files to a subdirectory
by tachyon (Chancellor) on Jul 13, 2001 at 11:59 UTC

    Hi Joe, here is a minimally modified version that works fine. The rename will fail if the target dir does not exist so I have added a line to make it if it does not.

    #!/usr/bin/perl -w use strict; my $dir = "c:/test/"; my $from = '.pl'; my $to = 'A.pl'; my $newdir = "c:/test/test/"; # make the target dir if it does not exist unless (-d $newdir) { mkdir $newdir or die "Unable to make dir $newdir $!\n"; } my $count = 0; while (<$dir*$from>) { my ($old,$new,$full); $full = $_; $new = $old = substr $full, length $dir; $new =~ s/$from$/$to/; print "Full name of original file is $full \n"; print "Name of original file is $old \n"; print "Full name of new file is $newdir$new \n"; $count++ if rename $full, $newdir.$new or die "Error renaming $!\n +"; } print "$count Files renamed!" if $count; # everything after the __END__ token is ignored by Perl __END__ # here is script with comments
Re: Renaming and moving files to a subdirectory
by Chady (Priest) on Jul 13, 2001 at 10:56 UTC

    umm.. let's see... here's a modified version with comments:

    #!/usr/bin/perl -w use strict; my $dir = "c:/Xwords/"; # my $n=length "c:/Xwords/"; # why do you need this? my $newDir = "c:/Xwords/PrintedXwords/"; my @files; # you're better off doing this: opendir DIR, $dir or die "Cannot open directory: $!\n"; @files = grep {/\.puz$/i} readdir (DIR); close DIR; foreach my $file (@files) { my $new = $file; my $from = $dir . $file; $new =~ s/\.puz$/A\.puz/; my $to = $newDir . $new; print " \n \n"; print " full name of original file is $from \n"; print " name of original file is $file \n"; print " full name of new file is $to\n"; print " \n \n"; rename $from, $to; } print "Files renamed !";

    Update: fixed declarations.


    He who asks will be a fool for five minutes, but he who doesn't ask will remain a fool for life.

    Chady | http://chady.net/
      Many thanks Chady, this was my first effort at developing a script, and using another snippet.

      Joe

      Your prose: " He who asks will be a fool for five minutes, but he who doesn't ask will remain a fool for life."
      Very applicable in my case!!

      Sorry Chady,the compilation was aborted because of 3 errors "Requiring Explicit Package Names" :
      @files at line 11
      $file at line 14
      $newDir at line 18

      Can you shed some light on this for me please?

      Many thanks,
      Joe, Townsville