944,181 Members | Top Members by Rank

Ad:
  • Perl Discussion Thread
  • Unsolved
  • Views: 35818
  • Perl RSS
Aug 10th, 2006
0

Determine File Extension Type

Expand Post »
Hello!
I am using the following PERL code to display the contents of a sub-directory on my server:
Perl Syntax (Toggle Plain Text)
  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???
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
whitetigereyes is offline Offline
1 posts
since Aug 2006
Aug 12th, 2006
0

Re: Determine File Extension Type

Quote ...
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
Reputation Points: 246
Solved Threads: 67
Practically a Posting Shark
KevinADC is offline Offline
898 posts
since Mar 2006
Aug 12th, 2006
0

Re: Determine File Extension Type

Quote ...
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.
Reputation Points: 246
Solved Threads: 67
Practically a Posting Shark
KevinADC is offline Offline
898 posts
since Mar 2006
Aug 12th, 2006
0

Re: Determine File Extension Type

Quote ...
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
Reputation Points: 246
Solved Threads: 67
Practically a Posting Shark
KevinADC is offline Offline
898 posts
since Mar 2006

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Perl Forum Timeline: need for help
Next Thread in Perl Forum Timeline: Using File::Basename to get file extensions





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC