LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 05-07-2007, 04:10 PM   #1
Four
Member
 
Registered: Aug 2005
Posts: 298

Rep: Reputation: 30
Perl: Browse files


In Perl how may I generate a list of all the files in a directory and store it in an array?

Thanks
 
Old 05-07-2007, 05:43 PM   #2
indienick
Senior Member
 
Registered: Dec 2005
Location: London, ON, Canada
Distribution: Arch, Ubuntu, Slackware, OpenBSD, FreeBSD
Posts: 1,853

Rep: Reputation: 65
Code:
#!/usr/bin/perl -w

@listOfFiles = glob("*.*");

foreach $item (@listOfFiles) { print "$item\n"; }
 
Old 05-07-2007, 08:11 PM   #3
twantrd
Senior Member
 
Registered: Nov 2002
Location: CA
Distribution: redhat 7.3
Posts: 1,440

Rep: Reputation: 52
Code:
#!/usr/bin/perl

my @files=`ls /directory/`;
print @file;
-twantrd
 
Old 05-07-2007, 09:25 PM   #4
bartonski
Member
 
Registered: Jul 2006
Location: Louisville, KY
Distribution: Fedora 12, Slackware, Debian, Ubuntu Karmic, FreeBSD 7.1
Posts: 443
Blog Entries: 1

Rep: Reputation: 48
This being perl, there's more than one way to do it... there are a couple more options which may give more control over directory access:

readdir() will yield a list of files in an array (when called in list context). You'll have to call opendir() first.

FILE::Find acts somewhat like the unix program find; it will traverse a directory tree, and execute a specified function on each file in the tree.

Both readdir() and File::Find are powerful and sophisticated, and require setup to use, but pay off in that they give an amazing amount of control over which files are processed and how.

I could give examples of how to use both, but they wouldn't do justice to the power of these functions.

Check out

Code:
perldoc File::Find
and

Code:
perldoc perlfunc

[then search for opendir and readdir]

Last edited by bartonski; 05-07-2007 at 09:44 PM.
 
Old 05-08-2007, 04:20 AM   #5
bigearsbilly
Senior Member
 
Registered: Mar 2004
Location: england
Distribution: Mint, Armbian, NetBSD, Puppy, Raspbian
Posts: 3,515

Rep: Reputation: 239Reputation: 239Reputation: 239
too much DOS

*.* isn't much good as it wont find files without a '.'
 
Old 05-08-2007, 03:28 PM   #6
indienick
Senior Member
 
Registered: Dec 2005
Location: London, ON, Canada
Distribution: Arch, Ubuntu, Slackware, OpenBSD, FreeBSD
Posts: 1,853

Rep: Reputation: 65
Very true - thanks bigearsbilly. I feel a little silly right now.
Code:
@listOfFiles = glob("*");
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Browse xp share files ASTRAPI Linux - Networking 2 03-20-2007 07:15 PM
How to browse shared files chus_84 Linux - Networking 1 02-14-2007 07:41 AM
perl files download instead of parsed on apache2 mod-perl2 not parsing perl files zeigerpuppy Debian 1 02-16-2006 05:31 AM
PERL script that can browse the file system... vous Programming 1 02-09-2004 01:02 PM
perl cgi /user browse fs j-ray Programming 1 09-15-2002 10:54 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 08:01 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration