Reputation: 111
I have a problem with this code. It matches the directories .
and ..
but not the others. Can you explain me why?
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);
Upvotes: 0
Views: 57
Reputation: 126722
The names in @files
, as you seem to be aware, don't have any path information, which means the -d
looks for them in the current working directory, fails to find them, and reports false. The same would happen with an -e
(exists) test.
You should alter your code like this. Note that identifiers that start with capital letters are reserved for global identifiers. It is also a bad idea to use bareword file and directory handles -- lexical handles are current best practice.
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;
Upvotes: 6
Reputation: 53478
OK, so a quick test on your code - I've mocked up a directory structure. Looks like you're 'doing it windows style?'
I'm using this:
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);
And I've created a directory structure looking like this:
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
And indeed - my 'SubDir' doesn't get >>>>
prefixed in the output.
File: .
>>>>.
File: ..
>>>>..
File: notajpg.jpg
file... notajpg.jpg
File: SubDir
File: wibble.txt
So your .jpg
bit is working fine. But where '.' and '..' pick up as dirs, 'SubDir' does not.
The reason seems to be related to path. .
and ..
exist in every directory, so that test will work regardless of script path.
You need to test against ( -d "$Checked_directory\\$fileToTest" )
Giving:
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);
Upvotes: 1