Tolgar
asked on
How to find the list of certain files in a given directory in Perl?
How can I find the list of certain files in a given directory and push them into an array?
Sudo Code:
Sudo Code:
findFiles($dir, $givenFileType){
for my $file (each file in the dirctory){
$fileType = get the file type of the $file
if $fileType eq $givenFileType{
push (@fileList, $file)
}
}
}
Simply do
push @fileList, <*.$givenFileType>;
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Any reason you didn't like my efficient solution instead of a link to a documentation page?
ASKER
Because it does not satisfy my requirements. It was a link but the link had exactly what I asked for.
When I looked at your solution, I cannot see anything that's related to a directory. I cannot specify a directory and also it does not search all the files in the subdirectories.
Sorry,
Thanks,
When I looked at your solution, I cannot see anything that's related to a directory. I cannot specify a directory and also it does not search all the files in the subdirectories.
Sorry,
Thanks,
You didn't mention anything about subdirectories in your requirements or pseudo code.
is a shorthand way of writing
push @fileList, <$dir/*.$givenFileType>;
is a shorthand way of writing
foreach my $file (<$dir*./$givenFileType>) {
push @fileList, $file;
}
push @fileList, <$dir/*.$givenFileType>;
is a shorthand way of writing for my $file ( <$dir/*> ){
$fileType = (fileparse $file,qr/(?<=\.)\Q$givenfiletype\E/)[-1];
if( $fileType eq $givenFileType ){
push (@fileList, $file)
}
}
ASKER
Thanks for the detailed info.
ASKER
I use Perl v5.8.8
Thanks,