Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

file case change

by costas (Scribe)
on Mar 15, 2002 at 12:06 UTC ( #151957=perlquestion: print w/replies, xml ) Need Help??
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.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://151957]
Approved by root
help
Chatterbox?
[Corion]: ... transfers in the background. Now that I think about it, this maybe even means that I can run the OpenGL filters on Youtube input :)
[ambrus]: Corion: I mentioned that the unix event loop of Prima always wakes up at least once every 0.2 seconds. Have you found out whether the win32 event loop of Prima does that too?
[Corion]: ambrus: Hmm - I would assume that the onDestroy message is sent from the destructor and doesn't go through the messageloop, but maybe it is sent when a window gets destroyed but all components are still alive...
[ambrus]: Corion: partly deep source dive, partly just conservative coding even if it adds an overhead.
[Corion]: ambrus: Hmm - no, I haven't looked at wakeup intervals ... I wonder why it should want to wakeup periodically because it gets a lot of messages from the Windows message loop (on Windows obviously)
[ambrus]: (Alternately a deep source dive and then rewrite that event loop to make it better, and then as a bonus you get an idle method.)
[ambrus]: The 0.2 seconds wakeup is likely a workaround for some bug, but I can't guess what bug that is.
[ambrus]: It's been there since Prima 1.00 iirc
[Corion]: Hmmm... Weird. Maybe it needs that for doing its timers or something. Still weird.
[Corion]: But I guess I'll find out soonish, as my application seems to use Prima for more than just displaying the OpenGL output soonish ;))

How do I use this? | Other CB clients
Other Users?
Others examining the Monastery: (8)
As of 2016-12-09 10:31 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    On a regular basis, I'm most likely to spy upon:













    Results (150 votes). Check out past polls.