954,525 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Determine File Extension Type

Hello!
I am using the following PERL code to display the contents of a sub-directory on my server:

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>";
}
}


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???

whitetigereyes
Newbie Poster
1 post since Aug 2006
Reputation Points: 10
Solved Threads: 0
 
1. Get perl to recognize the difference between a file and a sub-directory

File test operators are your friend: http://perldoc.perl.org/functions/-X.html

KevinADC
Posting Shark
921 posts since Mar 2006
Reputation Points: 246
Solved Threads: 67
 
2. Get perl to recognize the different file extensions.


There are a variety of ways to determine the file extension, you could use a regexp, or split(), or substr(), and probably more. But there is a core module for this purpose: File::Basename

using your existing code as an example:

<strong>use File::Basename;</strong>
$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) {
      <strong>my(undef, undef, $ftype) = fileparse($dirfile,qr{\..*});</strong>
      print "$ftype";
   }
}
KevinADC
Posting Shark
921 posts since Mar 2006
Reputation Points: 246
Solved Threads: 67
 
I would like to display .html files in green, .jpg files in blue and so on..

Building on the last example, I will use a hash table to store the color associated with the extension and the File::Basename module to get the extension and apply the color.

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{\..*});
      <strong>my $color = $colors{lc($ftype)} || 'black';</strong>
      print qq~<span style="color:$color">$dirfile</span>\n~;
   }
}


** apply your own HTML code to the output that gets printed, mine is just for the purpose of the

KevinADC
Posting Shark
921 posts since Mar 2006
Reputation Points: 246
Solved Threads: 67
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You