Gentoo Forums
Gentoo Forums
Gentoo Forums
Quick Search: in
Get a list of files with Perl
View unanswered posts
View posts from last 24 hours

 
Reply to topic    Gentoo Forums Forum Index Portage & Programming
View previous topic :: View next topic  
Author Message
ryan83vt
Guru
Guru


Joined: 28 Oct 2002
Posts: 370
Location: Blacksburg, VA

PostPosted: Thu Jan 30, 2003 8:48 pm    Post subject: Get a list of files with Perl Reply with quote

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
View user's profile Send private message
doug-x07
Tux's lil' helper
Tux's lil' helper


Joined: 16 Nov 2002
Posts: 122
Location: Paris, France

PostPosted: Thu Jan 30, 2003 9:32 pm    Post subject: Reply with quote

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
View user's profile Send private message
Matje
l33t
l33t


Joined: 29 Oct 2002
Posts: 619
Location: Hasselt, Belgium

PostPosted: Fri Jan 31, 2003 11:47 am    Post subject: Reply with quote

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
View user's profile Send private message
vericgar
Retired Dev
Retired Dev


Joined: 13 Dec 2002
Posts: 79
Location: Spokane, WA

PostPosted: Sat Feb 01, 2003 3:28 pm    Post subject: Reply with quote

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
View user's profile Send private message
avendesora
Veteran
Veteran


Joined: 16 Aug 2002
Posts: 1739
Location: Betelgeuse vicinity

PostPosted: Sat Feb 01, 2003 4:09 pm    Post subject: Reply with quote

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
View user's profile Send private message
ryan83vt
Guru
Guru


Joined: 28 Oct 2002
Posts: 370
Location: Blacksburg, VA

PostPosted: Sat Feb 01, 2003 7:30 pm    Post subject: Reply with quote

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
View user's profile Send private message
Matje
l33t
l33t


Joined: 29 Oct 2002
Posts: 619
Location: Hasselt, Belgium

PostPosted: Sat Feb 01, 2003 9:10 pm    Post subject: Reply with quote

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
View user's profile Send private message
idl
Retired Dev
Retired Dev


Joined: 24 Dec 2002
Posts: 1728
Location: Nottingham, UK

PostPosted: Sat Feb 01, 2003 9:58 pm    Post subject: Reply with quote

and for your array:

Code:
foreach $photo (@files)
{
# do shit with $photo here
}


can be handy.
Back to top
View user's profile Send private message
ryan83vt
Guru
Guru


Joined: 28 Oct 2002
Posts: 370
Location: Blacksburg, VA

PostPosted: Sun Feb 02, 2003 5:59 am    Post subject: Reply with quote

Thanks all who helped I have IMO the best photo album I have made.

If you care to view the site is
http://rsilva.campus.vt.edu/pics.php

If you would like to see the source code that I used
http://rsilva.campus.vt.edu/pics.php.txt

Anyone else who would like to use any of this code is more than welcome to it.
Back to top
View user's profile Send private message
Display posts from previous:   
Reply to topic    Gentoo Forums Forum Index Portage & Programming All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum