in reply to Parsing a directory

readdir() will give you the file name (leaf name). For the file size you can use the stat() function.

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
    Just a quick question to verify my thinking with respect to pre-pending the parent directory. I think I found an exception to this rule. If the parent is the root directory (/) then it is a special case because I get //somefile.name. Correct?
      No - that case is no different. The values returned by readdir() will not contain any leading slash. Just try it:
      opendir(D, "/"); for (readdir(D)) { print "got: $_\n" } closedir(D);