in reply to Re3: How can I opendir, replace all files containing a space with .?
in thread How can I opendir, replace all files containing a space with .?

I have:
opendir(DIR, "$dir") || die "cant open $dir $!\n"; foreach my $filename (readdir(DIR)) { if ($filename =~ / /) { my $newfile = $filename; $newfile =~ s/ +/./g; rename($filename, $newfile) unless $filename eq $newfile; } } closedir(DIR);
print "$newfile\n"; presents the correct name but no changes occur in the directory DIR. That's why I tried implementing the open function. Perhaps rename isn't working properly?
  • Comment on Re: Re3: How can I opendir, replace all files containing a space with .?
  • Download Code

Replies are listed 'Best First'.
Re5: How can I opendir, replace all files containing a space with .?
by Hofmator (Curate) on Aug 15, 2001 at 20:50 UTC

    Ok, try the changedir suggestion from VSarkiss (elsewhere in this thread) at the top of your script. And check the return value of rename like this: rename($filename, $newfile) or warn "Couldn't rename: $filename -> $newfile: $!";

    If you get the warning messages even with the chdir, then you have to put a full pathname as arguments for the rename. You can get them if $dir contains your path by putting the filename and pathname together like this: "$dir/$filename"

    -- Hofmator

      Ah, the chdir was what I was missing. It works now. Thanks a lot for your help!