in reply to 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 .?

Thanks! I appreciate the dissection. Maybe I'm just missing something, but while my script now does create a file with the . instead of the space, that file is empty. So its doing a "touch" rather than a "mv" and that is using open(NEW, ">>Test/$newfile"); what am I missing?
  • Comment on Re: Re: How can I opendir, replace all files containing a space with .?

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

    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

      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