http://www.perlmonks.org?node_id=151957

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

can anybody tell me why this doesnt work. I amjust trying to change to lower case the names of my files
#!/usr/bin/perl -w BEGIN { $|=1; print "Content-type: text/html\n\n"; use CGI::Carp('fatalsToBrowser'); } use strict; my $file; opendir (DIR, "/home/costas/test") or die "cannot opendir dir"; foreach $file (readdir(DIR)) { rename($file, lc($file)); print "$file<br>"; } closedir (DIR);
the files have been chmod at 777 and the print statement prints out the filenames fine. Can anyone see the mistake?

costas

Replies are listed 'Best First'.
Re: file case change
by Juerd (Abbot) on Mar 15, 2002 at 12:12 UTC

    opendir (DIR, "/home/costas/test") or die "cannot opendir dir"; foreach $file (readdir(DIR)) { rename($file, lc($file));

    That doesn't work, because you get only the filenames from readdir and not their paths. You will have to prefix "/home/costas/test/" yourself:

    my $path = '/home/costas/test'; opendir (DIR, $path) or die "Can't opendir: $!"; for (readdir DIR) { rename("$path/$_", "$path/" . lc); } closedir DIR;

    U28geW91IGNhbiBhbGwgcm90MTMgY
    W5kIHBhY2soKS4gQnV0IGRvIHlvdS
    ByZWNvZ25pc2UgQmFzZTY0IHdoZW4
    geW91IHNlZSBpdD8gIC0tIEp1ZXJk
    

Re: file case change
by ChOas (Curate) on Mar 15, 2002 at 12:12 UTC
    Does this give you a hint ? :
    rename($file, lc($file)) or print "Couldn't rename $file: $!\n";

    GreetZ!,
      ChOas

    print "profeth still\n" if /bird|devil/;
Re: file case change
by tachyon (Chancellor) on Mar 15, 2002 at 14:04 UTC

    You need something like this:

    $path = 'c:/TEST'; opendir DIR, $path or die "Cant open $path, perl says $!\n"; while (my $file = readdir DIR) { next if -d "$path/$file" or -l "$path/$file"; $lc_name = lc $file; rename "$path/$file", "$path/$lc_name" or print STDERR "Can't rena +me $path/$file, perl says $!\n"; }

    This avoids 1)lowercasing the whole path (won't exist) and 2) skips dirs and symlinks and lc's all else

    Update

    Added or print STDERR.... for ChOas ;-)

    cheers

    tachyon

    s&&rsenoyhcatreve&&&s&n.+t&"$'$`$\"$\&"&ee&&y&srve&&d&&print

Re: file case change
by costas (Scribe) on Mar 15, 2002 at 12:22 UTC
    I cant seem to get this working. I have been trying for ages but with no succes. the results are printed below
    #!/usr/bin/perl -w BEGIN { $|=1; print "Content-type: text/html\n\n"; use CGI::Carp('fatalsToBrowser'); } use strict; my $file; my $path = '/home/costas/test'; opendir (DIR, $path) or die "Can't opendir: $!"; for (readdir DIR) { print "$_<br>"; rename("$path/$_", lc("$path/$_")) or print "Couldn't rename $file +: $!\n"; } closedir DIR;
    Couldn't rename : Not a directory .. Couldn't rename : Device or resource busy safdLLP.txT.txt Couldn't rename : Permission denied safdLLPt.txT.txt Couldn't rename : Permission denied
    can anybody see why this is happening? I tried to add the path and it still doesnt work?
      Okay:
      change
      for (readdir DIR) { print "$_<br>"; rename("$path/$_", lc("$path/$_")) or print "Couldn't rename $file +: $!\n"; }
      to:
      for (readdir DIR) { next if (/^\.{1,2}$/); print "$_<br>"; rename("$path/$_", lc("$path/$_")) or print "Couldn't rename $_: $ +!\n"; }

      GreetZ!,
        ChOas

      print "profeth still\n" if /bird|devil/;

        This is really bad code. First you lowercase the whole file path ensuring that any uppercase path will break this code (as when you lowercase it you will then try to write along a non existant path). Next you seem to have forgotten that '.' and '..' are not the only directories you might find. Finally you should never print unescaped arbitrary strings in HTML.

        cheers

        tachyon

        s&&rsenoyhcatreve&&&s&n.+t&"$'$`$\"$\&"&ee&&y&srve&&d&&print

      Changing:

      rename("$path/$_", lc("$path/$_")) or print "Couldn't rename $file: $!\n";

      To

      rename("$path/$_", "$path" . lc("/$_")) or print "Couldn't rename $file: $!\n";

      Would allow upper case letters in your path.

      This page is intentionally left justified.