당신의 ./emails
디렉토리에이 파일이 포함 된 경우 :
1.msg
2.msg
3.msg
다음 @files
이름을 바꾸기 전에 ('.', '..', '1.msg', '2.msg', '3.msg')
하지만 rename
같은 것을 chdir
할 수 있도록 'emails/1.msg'
같은 이름, 'emails/2.msg'
등을 원하는 모양 :
를
chdir('emails');
for (@files) {
#...
}
아마도 ch chdir
반환 값도 확인하십시오.
또는 추가 디렉토리 이름 자신을 :
rename('emails/' . $old, 'emails/' . $_) or print "Error renaming $old: $!\n";
# or rename("emails/$old", "emails/$_") if you like string interpolation
# or you could use map if you like map
당신은 grep
를 사용하여 디렉토리의 읽기 및 필터링을 결합 할 수 있습니다 :
my @files = grep { /^RE .+msg$/ } readdir(DIR);
또는이 :
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);
당신은 무엇이 잘못의 아이디어를 얻을 수있는 오류 ($!)를 인쇄하는 경우 ... – pilcrow
그 지적 주셔서 감사합니다. 오류는 'No such file or directory'입니다. 나는 $ old 파일 이름이 정확하기 때문에 놀랍다. – Johnathan1
'rename '은 플랫폼 간 지원을 즐기지 않는 반면,'File :: Copy'의'move' 함수는 @Zaid : – Zaid