I have not tested it
I wish you did because it does not work the way you expect, if at all.

First, you redefine the perl built-in rename. So a call to rename would resolve to CORE::rename(), not the one you just wrote.

Second, you use neither strict nor warnings, which would have indicated:

Not enough arguments for rename at line 22, near "$eachFile)"
since rename takes a list of two elements as input.

Third, you're using a prototype. Don't. Read When to use Prototypes?, and I quote from a node written by tilly: Prototypes have many silent, nasty and unexpected side-effects..

Last, and most importantly, it does not work (even after correcting the above errors):

use strict; use warnings; sub re_name { my $path = $_[0]; #append a trailing / if it's not there if($path !~ /\/$/) { $path .= '/'; } #loop through the files contained in the directory for my $eachFile (glob($path.'*')) { #if the file is a directory if(-d $eachFile) { #pass the directory to the routine ( recursion ) re_name($eachFile); } else { my @strings = split('/', $eachFile); my $fileName = $strings[$#strings]; my $filePath = '/'.join('/', @strings[2..$#strings-1]); system("mv $filePath/$fileName $filePath/newfile"); } } } re_name('.'); re_name('/tmp/654653');
$ mkdir -p /tmp/654653 $ cd /tmp/654653 $ mkdir -p FOO/BAR $ mkdir -p FOO/TZE $ touch FOO/BAR/BANANA $ touch FOO/BAR/APPLE $ touch FOO/TZE/ORANGE $ perl 654653.pl mv: cannot stat `//654653.pl': No such file or directory mv: cannot stat `/BAR/APPLE': No such file or directory mv: cannot stat `/BAR/BANANA': No such file or directory mv: cannot stat `/TZE/ORANGE': No such file or directory mv: cannot stat `/654653/654653.pl': No such file or directory mv: cannot stat `/654653/FOO/BAR/APPLE': No such file or directory mv: cannot stat `/654653/FOO/BAR/BANANA': No such file or directory mv: cannot stat `/654653/FOO/TZE/ORANGE': No such file or directory
I don't want to disencourage from sharing your code, I just wish you tested it first. A working solution to the original problem could be:
use strict; use warnings; use File::Find; my $path = '.'; finddepth(\&pattern, $path); sub pattern { print "$_\n"; rename($_, lc($_)); } __END__
--
Andreas

In reply to Re^2: rename subdirectories by andreas1234567
in thread rename subdirectories by shness

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.