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

In what way are you doing an open(NEW, ">>Test/$newfile") call - I thought you are renaming files with rename??

Could you post the code you have now, please.

-- Hofmator

  • Comment on Re3: How can I opendir, replace all files containing a space with .?
  • Download Code

Replies are listed 'Best First'.
Re: Re3: How can I opendir, replace all files containing a space with .?
by nkpgmartin (Sexton) on Aug 15, 2001 at 20:37 UTC
    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?

      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!