| | |
Determine File Extension Type
Please support our Perl advertiser: Programming Forums - DaniWeb Sister Site
![]() |
•
•
Join Date: Aug 2006
Posts: 1
Reputation:
Solved Threads: 0
Hello!
I am using the following PERL code to display the contents of a sub-directory on my server:
That code simply opens a directory and spits out the contents (files and sub directories in alphabetical order) -
I was wondering how I could:
1. Get perl to recognize the difference between a file and a sub-directory
2. Get perl to recognize the different file extensions.. I would like to display .html files in green, .jpg files in blue and so on..
Or maybe someone can point me to a tutorial that will help with this type of issue???
I am using the following PERL code to display the contents of a sub-directory on my server:
Perl Syntax (Toggle Plain Text)
sub doit { opendir(DIRHANDLE,"/home/username/public_html/$DirName"); @filenames = ( sort readdir(DIRHANDLE) ); foreach $dirfile (@filenames) { print "<b><a href='/$DirName/$dirfile' target='new'>$dirfile</a></b><br><br>"; } }
That code simply opens a directory and spits out the contents (files and sub directories in alphabetical order) -
I was wondering how I could:
1. Get perl to recognize the difference between a file and a sub-directory
2. Get perl to recognize the different file extensions.. I would like to display .html files in green, .jpg files in blue and so on..
Or maybe someone can point me to a tutorial that will help with this type of issue???
•
•
•
•
1. Get perl to recognize the difference between a file and a sub-directory
http://perldoc.perl.org/functions/-X.html
•
•
•
•
2. Get perl to recognize the different file extensions.
using your existing code as an example:
use File::Basename;
$dir = '/home/username/public_html/';
$DirName = 'images';
doit();
sub doit {
opendir(DIRHANDLE,"$dir$DirName") or die "Can't open $dir$DirName: $!";
@filenames = sort readdir(DIRHANDLE);
close(DIRHANDLE);
foreach $dirfile (@filenames) {
my(undef, undef, $ftype) = fileparse($dirfile,qr{\..*});
print "$ftype<br>";
}
} Last edited by KevinADC; Aug 12th, 2006 at 2:31 am.
•
•
•
•
I would like to display .html files in green, .jpg files in blue and so on..
use File::Basename;
$dir = '/home/username/public_html/';
$DirName = 'images';
my %colors = (
'.html' => 'green',
'.jpg' => 'blue',
'.gif' => 'blue',
'.txt' => 'red',
);
doit();
sub doit {
opendir(DIRHANDLE,"$dir$DirName") or die "Can't open $dir$DirName: $!";
@filenames = sort readdir(DIRHANDLE);
close(DIRHANDLE);
foreach $dirfile (@filenames) {
my(undef, undef, $ftype) = fileparse($dirfile,qr{\..*});
my $color = $colors{lc($ftype)} || 'black';
print qq~<span style="color:$color">$dirfile</span><br>\n~;
}
}** apply your own HTML code to the output that gets printed, mine is just for the purpose of the
![]() |
Similar Threads
- File extension: How to display in Hexadecimal? (Windows NT / 2000 / XP)
- Need help w/ file extension (Windows NT / 2000 / XP)
- Creating own file extension (Computer Science)
Other Threads in the Perl Forum
- Previous Thread: need for help
- Next Thread: Using File::Basename to get file extensions
| Thread Tools | Search this Thread |
Tag cloud for Perl





