If your ./emails
directory contains these files:
1.msg 2.msg 3.msg
then your @files
will look something like ('.', '..', '1.msg', '2.msg', '3.msg')
, but your rename
wants names like 'emails/1.msg'
, 'emails/2.msg'
, etc. So you can chdir
before renaming:
chdir('emails'); for (@files) {
You might also want to check the return value of chdir
.
Or add directory names yourself:
rename('emails/' . $old, 'emails/' . $_) or print "Error renaming $old: $!\n";
You might want to combine reading and filtering your directory with grep
:
my @files = grep { /^RE .+msg$/ } readdir(DIR);
or even this:
opendir(DIR, 'emails') or die "Cannot open directory"; for (grep { /^RE .+msg$/ } readdir(DIR)) { (my $new = $_) =~ s/^RE //; rename("emails/$_", "emails/$new") or print "Error renaming $_ to $new: $!\n"; } closedir(DIR);
mu is too short
source share