Determine File Extension Type

Please support our Perl advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Aug 2006
Posts: 1
Reputation: whitetigereyes is an unknown quantity at this point 
Solved Threads: 0
whitetigereyes whitetigereyes is offline Offline
Newbie Poster

Determine File Extension Type

 
0
  #1
Aug 10th, 2006
Hello!
I am using the following PERL code to display the contents of a sub-directory on my server:
  1. sub doit {
  2. opendir(DIRHANDLE,"/home/username/public_html/$DirName");
  3. @filenames = ( sort readdir(DIRHANDLE) );
  4. foreach $dirfile (@filenames) {
  5. print "<b><a href='/$DirName/$dirfile' target='new'>$dirfile</a></b><br><br>";
  6. }
  7. }

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???
Reply With Quote Quick reply to this message  
Join Date: Mar 2006
Posts: 898
Reputation: KevinADC has a spectacular aura about KevinADC has a spectacular aura about 
Solved Threads: 67
KevinADC's Avatar
KevinADC KevinADC is offline Offline
Practically a Posting Shark

Re: Determine File Extension Type

 
0
  #2
Aug 12th, 2006
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
Reply With Quote Quick reply to this message  
Join Date: Mar 2006
Posts: 898
Reputation: KevinADC has a spectacular aura about KevinADC has a spectacular aura about 
Solved Threads: 67
KevinADC's Avatar
KevinADC KevinADC is offline Offline
Practically a Posting Shark

Re: Determine File Extension Type

 
0
  #3
Aug 12th, 2006
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:


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.
Reply With Quote Quick reply to this message  
Join Date: Mar 2006
Posts: 898
Reputation: KevinADC has a spectacular aura about KevinADC has a spectacular aura about 
Solved Threads: 67
KevinADC's Avatar
KevinADC KevinADC is offline Offline
Practically a Posting Shark

Re: Determine File Extension Type

 
0
  #4
Aug 12th, 2006
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{\..*});
      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
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the Perl Forum
Thread Tools Search this Thread



Tag cloud for Perl
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC