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 TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

how to list filename? 1

Status
Not open for further replies.

alan12

Programmer
Oct 8, 2002
3
US
how to use perl to list all file name under certain folder?
 
you can use backticks to send the names to an array and then print them:
Code:
@files = `ls`;

foreach $file (@files)
{
   print STDOUT "$file\n";
} --Derek

[i]"Fear not the storm for this is where we grow strong."[/i]
 
Hi alan12,

You also might want to consider using opendir, readdir and related functions. You can look them up in the perlfunc documentation.

You might also look at the 'File' modules for such things as 'File::Find'. If you search, you should find at least one useful thread on this subject.

Good luck,
Grant.
 
hi,
the simplest method is to glob the filenames like this:

@files = <*.*>;
@files = glob(&quot;*.*&quot;);
(replace *.* with whatever you want to match the search)

but for a more specific path($path) you can use readdir function like this:

opendir(DIR, $path);
@files = grep { /\.pl$/ } readdir(DIR);
closedir(DIR);
(this will match all perl(.pl) files)

---
To do a recursive search (i.e. to list all files in subfolders too, use this function)

use File::Find;
sub process_file {
# do whatever;
}
find(\&process_file, @DIRLIST);


---
san.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top