In File::Find you are given three variables:
$File::Find::dir is the current directory name,
$_ is the current filename within that directory
$File::Find::name is the complete pathname to the file.
So, you don't need File::Basename to parse the file and directory. Unless your files are on a different drive, you could safely give the path without the drive letter, C:.

marinersk pointed out the problem with your approach. A correct solution might be stated (below) using the 3 variables that come with File::Find.

Also notice that perl can read forward slashes on a Windows machine - it's not necessary to have backslashes.

This will rename all the files in folder1 and any subdirectories under it. (You might want to print out the proposed name changes before doing the operation to see that all is well.

#!/usr/bin/perl use strict; use warnings; use File::Find; find(\&wanted, '/folder1'); sub wanted { if (-f) { (my $new = $File::Find::name) =~ s/\//./g; print "$File::Find::dir/$new\n"; #rename $File::Find::name, "$File::Find::dir/$new" # or warn "Can't rename $File::Find::name\n"; } }

In reply to Re: rename file name in directory and subdir by Cristoforo
in thread rename file name in directory and subdir by starface245

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.