fad3r <[EMAIL PROTECTED]> wrote:

: Thank you for the response.  I am trying to incorporate that
: with the rename function.  I am not quite sure how to do that. 
: I am trying to loop over the files twice.  Here is what I have;
: can you tell me what I am doing wrong? 

    First, try to keep all replies on the list. I know it defaults
to the person sending the reply, but no amount of cajoling
will get the moderators to change this odd behavior. I generally
use reply to all and then delete the addresses of individuals. It
is a pain, but the archives will then show your solution and
others may find that solution useful.


    Second, always, always, always use strict and turn on
warnings. Do this in even the tiniest scripts. On some older
versions of perl you need to add a -w to the shebang line
instead of using the warnings module. Read perllexwarn and the
strict.pm docs for details.
 
use strict;
use warnings;


    Third, I applaud your check for 'opendir', but you didn't
use the $! variable which perl provides to give detailed
information about the error. Get out of the habit of adding a
new line character to the end of errors.

opendir( DIRECTORY, $dir ) || die qq(Cannot open "$dir": $!;


    Fourth, use more descriptive names than @files3, @files4,
etc. I assume there are also @files0, @files1 and @files2.
Start using descriptive names now. Creative naming will aid
you as you develop larger projects.


: #!/usr/bin/perl
: 
: print "\nWhere are the files?  ";
: $dir = <stdin>;
: chomp ($dir);

    We'll need to start using lexical variables from this
point forward. Lexical variables are limited in scope and
won't raise alarms while strict is in force. We can also
combine the last two statements.

chomp( my $dir = <stdin> );


: opendir (DIRECTORY, $dir) || die "cannot open directory\n";
: @files3 = readdir (DIRECTORY);

    Parenthesis are optional. Once the directory is
processed, it should be closed as soon as possible.

opendir DIRECTORY, $dir or die qq(Cannot open "$dir": $!);
my @files = readdir DIRECTORY;
closedir DIRECTORY;

: foreach $file3 (@files3) {
:       $file3 =~ tr/_/ /;
:       print $file3; #this has the right info but cant seem to
: get it to
: change the filenames.

    When sending scripts to an email list or help forum,
avoid or edit long lines. Trailing comments like this one
should be avoided. Personally, I never allow them in my
style. If you use them trim them down to a width less likely
to cause wrapping. Note that I also converted all tabs to
spaces before pasting to the message. Think before you paste.

    print $file3;       # this has the right info
                        # but cant seem to
                        # get it to change the
                        # filenames.

: }

    The filenames changed for me.

opendir DIRECTORY, $dir or die qq(Cannot open "$dir": $!);
my @files = readdir DIRECTORY;
closedir DIRECTORY;

foreach my $file ( @files ) {
    $file =~ tr/_/ /;
    print "$file\n";
}

    We can test this by changing @files to a list of
files names with appropriate underscores.

my @files = qw( foo_bar john_q bleep );

foreach my $file ( @files ) {
    $file =~ tr/_/ /;
    print "$file\n";
}

__END__

I get this.

foo bar
john q
bleep


: opendir (DIRECTORY, $dir) || die "cannot open directory\n";
: @files4 = readdir (DIRECTORY);
: foreach $file4 (@files4) {
:       rename("$dir/$file4", "$dir/$file3");
: }

    Q: Why use two loops?

foreach my $old_file_name ( @files ) {

    # duplicate name
    my $new_file_name = $old_file_name;

    # change duplicate
    $new_file_name =~ tr/_/ /;

    # report new name
    printf
        "Renaming: %s\n      to: %s\n\n",
            $old_file_name, $new_file_name;

    # change name
    rename "$dir/$old_file_name", "$dir/$new_file_name";
}

__END__


HTH,

Charles K. Clarkson
-- 
Mobile Homes Specialist
254 968-8328

_______________________________________________
Perl-Win32-Users mailing list
Perl-Win32-Users@listserv.ActiveState.com
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs

Reply via email to