View previous topic :: View next topic |
Author |
Message |
ryan83vt Guru
Joined: 28 Oct 2002 Posts: 370 Location: Blacksburg, VA
|
Posted: Thu Jan 30, 2003 8:48 pm Post subject: Get a list of files with Perl |
|
|
I want to run a script that will get a list of all the files in a directory and store them in an array. I will be using this to generate an online photo album, and I am going to need to do some processing with the filenames. I tried this:
Code: |
@list = system(ls);
|
but that doesn't return anything, just outputs the files.
edit: if I can do this in php also, that would be even better because then I could code it straight into the web page.
Thanks. |
|
Back to top |
|
|
doug-x07 Tux's lil' helper
Joined: 16 Nov 2002 Posts: 122 Location: Paris, France
|
Posted: Thu Jan 30, 2003 9:32 pm Post subject: |
|
|
There are loads of different ways of doing this. My favorites :
Code: | opendir(DIR, $dirname) or die "can't opendir $dirname: $!";
while (defined($file = readdir(DIR))) {
# process "$dirname/$file"
}
closedir(DIR); | That's the most efficient for processing all files in a directory. If you really must store it in an array before processing just do:
Code: | opendir(DIR, $dirname) or die "can't opendir $dirname: $!";
@files = readdir(DIR);
closedir(DIR); |
Alternatively if you want only files that match a given pattern try globbing it:
Code: | opendir(DIR, $dirname) or die "can't opendir $dirname: $!";
@files = grep { /\.jpg$/ } readdir(DIR);
closedir(DIR);
|
Check perldoc opendir, closedir, readdir, grep for more details. _________________ #! /usr/bin/perl
if( @first != $succeed ) {
post { $question->forum && eval '$answers' };
try { $again } catch { $problem && $resolve };
bless $posters; } |
|
Back to top |
|
|
Matje l33t
Joined: 29 Oct 2002 Posts: 619 Location: Hasselt, Belgium
|
Posted: Fri Jan 31, 2003 11:47 am Post subject: |
|
|
The php-way:
Code: |
$hoofdmap = opendir('./albums');
$albums=array ();
while ($submap = readdir($hoofdmap)) {
if ($submap != "." && $submap != "..") {
$albums[] = $submap;
end($albums);
print ('<a href="albums.php?view='. key($albums) .'&detail=1&page=0">'. $submap .'</a><br><br>');
}
}
closedir($hoofdmap);
|
This is a script I use to generate foto albums on the fly. It's just a part of it. The idea is, it reads a dir which contains subdirs, each subdir contains pictures, this is the part which puts the subdirs in an array...
edit: Just read that you need it for a photo album too, if you want I can post the complete files _________________ Life is like a box of chocolates... Before you know it, it's empty... |
|
Back to top |
|
|
vericgar Retired Dev
Joined: 13 Dec 2002 Posts: 79 Location: Spokane, WA
|
Posted: Sat Feb 01, 2003 3:28 pm Post subject: |
|
|
here's my "n00b hack" that most ppl dont like because it's not OS independant... (it shouldn't be too hard to tweak things to work on PHP also)
Code: |
@filesarray = split(/\n/, `ls -1 *.mp3`);
|
(I do a lot of quick hybrid bash/perl scripts for mp3 junk) _________________ +~+ Sometimes a good ole loving kick is all it needs +~+ |
|
Back to top |
|
|
avendesora Veteran
Joined: 16 Aug 2002 Posts: 1739 Location: Betelgeuse vicinity
|
Posted: Sat Feb 01, 2003 4:09 pm Post subject: |
|
|
My favorite is usually:
Code: |
my @files = glob($dir."/*.jpg");
|
Does the job pretty well, if you need full path info for your files. |
|
Back to top |
|
|
ryan83vt Guru
Joined: 28 Oct 2002 Posts: 370 Location: Blacksburg, VA
|
Posted: Sat Feb 01, 2003 7:30 pm Post subject: |
|
|
Matje, i'd like that except I think I want to do the album using a javascript popup window that will be the size of the image. How do you implement your album? If theres a more efficient way, I'm open to that. I'd like to do it in php cause then I can just put images in the folder and the album will be automatically updated. |
|
Back to top |
|
|
Matje l33t
Joined: 29 Oct 2002 Posts: 619 Location: Hasselt, Belgium
|
Posted: Sat Feb 01, 2003 9:10 pm Post subject: |
|
|
Check out http://www.lanzone.be ==> albums
There is only one album there atm and they are bad quality, due to limited hosting It uses the name of the subdirectory as the "title" of the album, going to implement the name of the picture as the title of the photo... Also, my files are on the server only in big format, the php-script generates thumbnails on the fly. I choose not to open them in a pop-up window or something like that, but it could easily be fixed... _________________ Life is like a box of chocolates... Before you know it, it's empty... |
|
Back to top |
|
|
idl Retired Dev
Joined: 24 Dec 2002 Posts: 1728 Location: Nottingham, UK
|
Posted: Sat Feb 01, 2003 9:58 pm Post subject: |
|
|
and for your array:
Code: | foreach $photo (@files)
{
# do shit with $photo here
} |
can be handy. |
|
Back to top |
|
|
ryan83vt Guru
Joined: 28 Oct 2002 Posts: 370 Location: Blacksburg, VA
|
|
Back to top |
|
|
|