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!

Listing files in a directory 1

Status
Not open for further replies.

naq2

Programmer
Aug 3, 2004
74
FR
How can I list the files and perform an action on eachone.

An algorism of what I want to do could be:
Code:
foreach FILE in DIRECTORY {
    PERFORM AN ACTION ON THE FILE
}

Thanks in advance.
 
Lots of different ways:
Code:
opendir(DIR, ".");
foreach (readdir(DIR)) {
    next unless -f;
    print "do something with $_\n";
}
closedir(DIR);
Or:
Code:
opendir(DIR, ".");
@files = grep {-f} readdir(DIR);
closedir(DIR);
foreach (@files) {
    print "do something with $_\n";
}
Or if you want to recurse down into subdirectories:
Code:
use File::Find;
sub wanted {
    if (-f) {
        print "do something with $File::Find::name\n";
    }
}
find(\&wanted, ".");
Cheers, Neil
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top