how to use Perl to rename the filenames and directory names under current and subdirectories recursi

Discussion in 'Perl Misc' started by lucy, Sep 1, 2004.

  1. lucy

    lucy Guest

    Is there a way to use Perl command line

    to rename all the filenames and directory names

    from '*abcd*.*' to '*xyz*.*'

    and do this recursively for all files and all subdirectories?

    Thanks a lot
     
    lucy, Sep 1, 2004
    #1
    1. Advertising

  2. "lucy" <> wrote in news:ch5bu2$s4b$:

    > Is there a way to use Perl command line
    >
    > to rename all the filenames and directory names
    >
    > from '*abcd*.*' to '*xyz*.*'
    >
    > and do this recursively for all files and all subdirectories?



    use File::Find;
     
    A. Sinan Unur, Sep 1, 2004
    #2
    1. Advertising

  3. "A. Sinan Unur" <> wrote:

    > "lucy" <> wrote in news:ch5bu2$s4b$:
    >
    >> Is there a way to use Perl command line
    >>
    >> to rename all the filenames and directory names
    >>
    >> from '*abcd*.*' to '*xyz*.*'
    >>
    >> and do this recursively for all files and all subdirectories?

    >
    >
    > use File::Find;


    I thought about replying with just "yes". After all, the question wasn't
    "how?", but "is there a way?"
     
    David K. Wall, Sep 2, 2004
    #3
  4. lucy

    Anno Siegel Guest

    lucy <> wrote in comp.lang.perl.misc:
    > Is there a way to use Perl command line
    >
    > to rename all the filenames and directory names
    >
    > from '*abcd*.*' to '*xyz*.*'
    >
    > and do this recursively for all files and all subdirectories?


    See the _Perl Cookbook_, a section titled "Renaming Files".

    Anno
     
    Anno Siegel, Sep 2, 2004
    #4
  5. On 1 Sep 2004 21:24:05 GMT, "A. Sinan Unur" <>
    wrote:

    >"lucy" <> wrote in news:ch5bu2$s4b$:
    >
    >> Is there a way to use Perl command line
    >>
    >> to rename all the filenames and directory names

    ^^^^^^^^^^^^^^^

    >> from '*abcd*.*' to '*xyz*.*'
    >>
    >> and do this recursively for all files and all subdirectories?

    >
    >
    >use File::Find;


    With some caution because of the above detail, i.e. if she really
    wants to also rename directory names (and there are some to be
    renamed), then something along the lines of

    find sub {
    rename $_, do {
    (my $n=$_) =~ s/abc/xyz/ or return;
    $n;
    }
    }, '.';

    which is the kind of solution one may naively think about, simply
    won't work. IMHO the best workaround would be to do the renaming in
    'preprocess', returning the updated list, and actually doing nothing
    in 'wanted'. All this is left as an exercise to the OP... ;-)


    Michele
    --
    you'll see that it shouldn't be so. AND, the writting as usuall is
    fantastic incompetent. To illustrate, i quote:
    - Xah Lee trolling on clpmisc,
    "perl bug File::Basename and Perl's nature"
     
    Michele Dondi, Sep 2, 2004
    #5
  6. Michele Dondi wrote:
    > On 1 Sep 2004 21:24:05 GMT, "A. Sinan Unur" <>
    > wrote:
    >
    >> "lucy" <> wrote in
    >> news:ch5bu2$s4b$:
    >>
    >>> Is there a way to use Perl command line
    >>>
    >>> to rename all the filenames and directory names

    > ^^^^^^^^^^^^^^^
    >
    >>> from '*abcd*.*' to '*xyz*.*'
    >>>
    >>> and do this recursively for all files and all subdirectories?

    >>
    >>
    >> use File::Find;

    >
    > With some caution because of the above detail, i.e. if she really
    > wants to also rename directory names (and there are some to be
    > renamed), [...]
    > which is the kind of solution one may naively think about, simply
    > won't work. IMHO the best workaround would be to do the renaming in
    > 'preprocess', returning the updated list, and actually doing nothing
    > in 'wanted'. All this is left as an exercise to the OP... ;-)


    Way to complicated. Just to a depth-first parsing and you will be fine.

    jue
     
    Jürgen Exner, Sep 2, 2004
    #6
  7. On Thu, 02 Sep 2004 14:37:53 GMT, "Jürgen Exner"
    <> wrote:

    >> won't work. IMHO the best workaround would be to do the renaming in
    >> 'preprocess', returning the updated list, and actually doing nothing
    >> in 'wanted'. All this is left as an exercise to the OP... ;-)

    >
    >Way to complicated. Just to a depth-first parsing and you will be fine.


    D'Oh! You're *perfectly* right... ;-)

    ....Though I wouldn't say "way too complicated" (just "more
    complicated"). It would be a matter of something similar to:

    find { preprocess =>
    sub {
    map {
    my $o=$_;
    rename $o, $_ or
    warn "Can't rename `$o' to `$_': $!\n"
    if s/abc/xyz/;
    $_;
    } @_;
    },
    wanted => sub {} }, '.';

    BTW, I must say that I have tried to avoid the 'wanted' line and I got
    an error, reasonably enough in terms of UI for logically it is
    expected that one would use the 'wanted' action on wanted files.

    Anyway I wonder wether it could be acceptable/desirable to have a
    default implicit C<<wanted => sub {}>> instead...


    Michele
     
    Michele Dondi, Sep 3, 2004
    #7
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. B.J.
    Replies:
    4
    Views:
    757
    Toby Inkster
    Apr 23, 2005
  2. Psymaster
    Replies:
    3
    Views:
    778
    Psymaster
    Jun 30, 2003
  3. rtilley
    Replies:
    22
    Views:
    670
    Christos Georgiou
    Mar 2, 2006
  4. Peter Jamieson

    Rename files using directory names

    Peter Jamieson, Nov 15, 2007, in forum: Perl Misc
    Replies:
    4
    Views:
    177
    John W. Krahn
    Nov 16, 2007
  5. vdvorkin
    Replies:
    0
    Views:
    423
    vdvorkin
    Feb 10, 2011
Loading...

Share This Page