in reply to Parsing a directory
What do you mean by "file type" - it could mean many things? Do you just need the suffix of the file name, or do you actually want to parse the contents of the file to see if its structure conforms to a list of known file structures. For the latter, have a look at File::MMagic.
One common trip-up for people using readdir() is that they forget that they need to prepend the parent directory path to obtain a complete path. This is especially true when passing the file name to another function. Here is a good idiom to follow:
opendir(D, $dir) or die "unable to read $dir: $!" while (defined(my $leaf = readdir(D))) { my $path = "$dir/$leaf"; # perhaps use File::Spec here ... some_function($path); # often passing $leaf here is a mistake ... } closedir(D);
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Parsing a directory
by gw1500se (Beadle) on Jun 29, 2008 at 00:10 UTC | |
by pc88mxer (Vicar) on Jun 29, 2008 at 01:51 UTC |