chdir($workdir) or die "Unable to enter dir $workdir:$!\n"; ... foreach my $name (@names){ ... if (-d $name){ &ScanDirectory($name); <-- This changes chdir. next; <-- Never changed back. } ... } ...

Fix:

use File::Spec::Functions qw( catfile ); # This subroutine takes the name of a directory and recursively scans # down the filesystem from that point looking for files named "core" sub ScanDirectory{ my $workdir = shift; if ($workdir =~ /^[a-zA-Z]:\z/) { $workdir .= '.'; # Fix bug in Windows. } print "Work dir = $workdir\n"; opendir(local *DIR, $workdir) or die "Unable to open $workdir: $!\n"; my @names = readdir(DIR) or die "Unable to read $workdir: $!\n"; closedir(DIR); foreach my $name (@names){ next if ($name eq "."); next if ($name eq ".."); my $fqname = catfile($workdir, $name); if (-d $fqname){ # is this a directory? ScanDirectory($fqname); next; } print "Name = $name\n"; $newName = $name; $newName =~ s/\..*//; $newName .= ".xml"; my $new_fqname = catfile($workdir, $newName); $result = rename($fqname, $new_fqname) or die "Cannot rename $fqname to $new_fqame: $!"\n; } } $|=1; # Turn off I/O buffering ScanDirectory($ARGV[0]);

Better yet, let's remove needless recursion.

use File::Spec::Functions qw( catfile ); # This subroutine takes the name of a directory and recursively scans # down the filesystem from that point looking for files named "core" sub ScanDirectory{ my $workdir = shift; if ($workdir =~ /^[a-zA-Z]:\z/) { $workdir .= '.'; # Fix bug in Windows. } my @todo = ( $workdir ); while (@todo) { my $workdir = shift(@todo); print "Work dir = $workdir\n"; opendir(local *DIR, $workdir) or die "Unable to open $workdir: $!\n"; my @names = readdir(DIR) or die "Unable to read $workdir: $!\n"; closedir(DIR); foreach my $name (@names){ next if ($name eq "."); next if ($name eq ".."); my $fqname = catfile($workdir, $name); if (-d $fqname){ # is this a directory? push @todo, $fqname; next; } print "Name = $name\n"; $newName = $name; $newName =~ s/\..*//; $newName .= ".xml"; my $new_fqname = catfile($workdir, $newName); $result = rename($fqname, $new_fqname) or die "Cannot rename $fqname to $new_fqame: $!"\n; } } } $|=1; # Turn off I/O buffering ScanDirectory($ARGV[0]);

Btw, I properly scoped DIR (added local *DIR;) and removed requests to ignore prototypes (removed &).

Update: Added fix.


In reply to Re: Perl rename method by ikegami
in thread Perl rename method by iman_saleh

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.