-d:不匹配我的文件夹
-d : not matching my folder
我对这段代码有疑问。它匹配目录 .
和 ..
但不匹配其他目录。你能解释一下为什么吗?
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);
@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;
好的,所以对您的代码进行快速测试 - 我模拟了一个目录结构。看起来你是 'doing it windows style?'
我正在使用这个:
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' 没有在输出中添加 >>>>
前缀。
File: .
>>>>.
File: ..
>>>>..
File: notajpg.jpg
file... notajpg.jpg
File: SubDir
File: wibble.txt
所以您的 .jpg
位工作正常。但是哪里 '。'和 '..' 作为 dirs 拾取,'SubDir' 没有。
原因好像和路径有关。 .
和 ..
存在于每个目录中,因此无论脚本路径如何,测试都可以进行。
您需要针对 ( -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);
我对这段代码有疑问。它匹配目录 .
和 ..
但不匹配其他目录。你能解释一下为什么吗?
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);
@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;
好的,所以对您的代码进行快速测试 - 我模拟了一个目录结构。看起来你是 'doing it windows style?'
我正在使用这个:
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' 没有在输出中添加 >>>>
前缀。
File: .
>>>>.
File: ..
>>>>..
File: notajpg.jpg
file... notajpg.jpg
File: SubDir
File: wibble.txt
所以您的 .jpg
位工作正常。但是哪里 '。'和 '..' 作为 dirs 拾取,'SubDir' 没有。
原因好像和路径有关。 .
和 ..
存在于每个目录中,因此无论脚本路径如何,测试都可以进行。
您需要针对 ( -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);