You already have gotten working but let me tell 'tear apart' your code, to tell you what went wrong. You should see this as a chance to learn, no offence meant.

I put some comments into your sourcecode, always above the line which they belong to:

# good, open with error checking opendir(DIR, "$dir") || die "can't open $dir $!\n"; # maybe name the variable @filenames - as there is more than one # or get rid of the variable altogether by saying # foreach my $oldname ( readdir(DIR) ) my @filename = readdir (DIR); # declare filename with my and give better name (see above) # foreach my $oldname (@filename) foreach $filename (@filename) { # this matches against the special variable $_ # (which you are not using) you want # if ($filename =~ / /) if (/ /) { # the regex is completely wrong you want either # s/ +/./g or tr/ /./ try to figure out the difference # and $newfile is never assigned a value so: my $newfile = $oldfile; $newfile =~ s/ +/./g; # you don't have to do the rename when you didn't # change anything, so rename($oldname, $newfile) unless $oldname eq $newfile; } } closedir(DIR);

If you want to use this as a standalone script, I'd go for the rename program out of the Cookbook (recipe 9.9):

#!/usr/bin/perl -w # rename - Larry's filename fixer $op = shift or die "Usage: rename expr [files]\n"; chomp(@ARGV = <STDIN>) unless @ARGV; for (@ARGV) { $was = $_; eval $op; die $@ if $@; rename($was,$_) unless $was eq $_; }
which can be used e.g. in the following ways (also from the Cookbook)
% rename 's/\.orig$//' *.orig % rename 'tr/A-Z/a-z/ unless /^Make/' * % rename '$_ .= ".bad"' *.f % rename 'print "$_: "; s/foo/bar/ if <STDIN> =~ /^y/i' * % find /tmp -name '*~' -print | rename 's/^(.+)~$/.#$1/'
This approach has several advantages, as you can specify the affected files from the command line (and don't have to work on all files in a directory). Furthermore it is very flexible due to the eval approach. Your problem could be solved with it like this:
% rename 's/ +/./g' * or % rename 'tr/ /./' *
Try to understand how this program works - and if you have any questions, feel free to ask here ...

-- Hofmator


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

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.