我的代码有问题。它匹配目录.
和..
,但不匹配其他目录。你能解释一下为什么吗?
my $Checked_directory = $Tested_directory . '\Sources';
opendir(Checked_directory, $Checked_directory)
or print STDOUT "\n 101 - Cant open $Checked_directory: $!\n";
@files = readdir(Checked_directory); #etablished the list of file in the checked folder
foreach my $fileToTest (@files) {
print $fileToTest . "\n"; #debug
if ($fileToTest =~ m/.jpg$/) {
print 'file... ' . $fileToTest . "\n";
$Localisation_file = $Tested_directory . '\Sources\\' . $fileToTest; #file to test
}
elsif (-d $fileToTest) {
print ">>>>" . $fileToTest . "\n";
}
}
closedir(Checked_directory);
答案 0 :(得分:6)
您似乎知道@files
中的名称没有任何路径信息,这意味着-d
在当前工作目录中查找它们,找不到它们,并且报告错误。 -e
(存在)测试会发生同样的情况。
您应该像这样更改您的代码。请注意,以大写字母开头的标识符是为全局标识符保留的。使用裸字文件和目录句柄也是一个坏主意 - 词法句柄是当前的最佳实践。
my $checked_directory = "$tested_directory\\Sources";
opendir $checked_dh, $checked_directory
or print STDOUT "\n 101 - Cant open $checked_directory: $!\n";
while (my $file_to_test = readdir $checked_dh) {
print "$file_to_test\n";
my $localisation_file = "$tested_directory\\Sources\\$file_to_test";
if (-d $localisation_file) {
print ">>>> $file_to_test\n";
}
elsif ($file_to_test =~ /\.jpg$/) {
print "file... $file_to_test\n";
}
}
closedir $checked_dh;
答案 1 :(得分:1)
好的,快速测试你的代码 - 我嘲笑了一个目录结构。看起来你是在做这种风格吗?'
我用这个:
use strict;
use warnings;
my $Tested_directory = 'C:\\temp';
my $Checked_directory = $Tested_directory . '\Sources';
opendir( Checked_directory, $Checked_directory )
or print STDOUT "\n 101 - Cant open $Checked_directory: $!\n";
my @files = readdir(Checked_directory)
; #etablished the list of file in the checked folder
foreach my $fileToTest (@files) {
print "File: $fileToTest \n"; #debug
if ( $fileToTest =~ m/.jpg$/ ) {
print 'file... ' . $fileToTest . "\n";
my $Localisation_file =
$Tested_directory . '\Sources\\' . $fileToTest; #file to test
}
else {
print "\n$fileToTest is not a jpg\n";
}
if ( -d $fileToTest ) {
print ">>>>" . $fileToTest . "\n";
}
}
closedir(Checked_directory);
我已经创建了一个如下所示的目录结构:
Directory of C:\temp\Sources
22/01/2015 13:07 <DIR> .
22/01/2015 13:07 <DIR> ..
22/01/2015 13:07 0 notajpg.jpg
22/01/2015 13:04 <DIR> SubDir
22/01/2015 13:07 6 wibble.txt
2 File(s) 6 bytes
确实 - 我的SubDir&#39;在输出中没有>>>>
前缀。
File: .
>>>>.
File: ..
>>>>..
File: notajpg.jpg
file... notajpg.jpg
File: SubDir
File: wibble.txt
所以你的.jpg
位工作正常。但是在哪里&#39;。&#39;和&#39; ..&#39;拿起dirs,&#39; SubDir&#39;才不是。
原因似乎与路径有关。每个目录中都存在.
和..
,因此无论脚本路径如何,测试都将起作用。
您需要针对( -d "$Checked_directory\\$fileToTest" )
,并提供:
use strict;
use warnings;
my $Tested_directory = 'C:\\temp';
my $Checked_directory = $Tested_directory . '\Sources';
opendir( Checked_directory, $Checked_directory )
or print STDOUT "\n 101 - Cant open $Checked_directory: $!\n";
my @files = readdir(Checked_directory)
; #etablished the list of file in the checked folder
foreach my $fileToTest (@files) {
print "File: $fileToTest \n"; #debug
if ( $fileToTest =~ m/.jpg$/ ) {
print 'file... ' . $fileToTest . "\n";
my $Localisation_file =
$Tested_directory . '\Sources\\' . $fileToTest; #file to test
}
elsif ( -d "$Checked_directory\\$fileToTest" ) {
print ">>>>" . $fileToTest . "\n";
}
}
closedir(Checked_directory);