Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Perl Directory Listing Windows/Unix

Status
Not open for further replies.

alfie002

Technical User
Mar 3, 2004
121
GB
Dear all,

I have been trying to track down a Perl Module that I can use to list directories ( similar to the FTP module (ls command)), on Unix and hopefully Win32, with the same code. I found one called File::Listing but cannot get it to work. Am I using the right one or should I be using more suitable ???

Thanks

Alf
 
Code:
opendir (DIR, "/directory/to/read");
print join ("\n", readdir(DIR));
closedir (DIR);

-------------
Kirsle.net | Kirsle's Programs and Projects
 
Thanks Kirsle's, I'll give this a go.

Alf
 
Kirsle's,

if I wanted to assign the results from

opendir (DIR, "/directory/to/read");
print join ("\n", readdir(DIR));
closedir (DIR);


to a global variable within the main script, would I do something like this;

@alfie002 = ( opendir (DIR, "/directory/to/read");
print join ("\n", readdir(DIR));
closedir (DIR);
)

or create a function and then assign the function to alfie002 ?

Any help appreciated

Thanks

Alf
 
Code:
opendir (DIR, "/directory/to/read");
my @files = readdir(DIR);
closedir (DIR);

readdir returns an array. ;-)

-------------
Kirsle.net | Kirsle's Programs and Projects
 

While on the subject, I would like to know if there is a way to ignore the "." and/or ".." assigned for the parent directories. I always used the grep function against the array to filter only real files or directory ? Any other more simple way to achieve this.

When I loop through the array I always get the
Code:
.
..
File01.fil
File2.fil

 
Just grep those files away.

Code:
opendir (DIR, "/directory/to/read");
my @files = grep {! /^\.\.?$/} readdir(DIR);
closedir (DIR);

Alternatively, you can use the CPAN File::Slurp module, which filters out . and .. by default.


Code:
use File::Slurp qw(read_dir);

my @files = read_dir("/directory/to/read");
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top