GThorne has asked for the wisdom of the Perl Monks concerning the following question:

I am using these statements in a loop to read all the files in a directory and make modifications to each file. My problem is that I am picking up subdirectoies also. How can I ID the subdirectories and skip them. I'm fairly new to perl please show me what the statement should be to skip the $file if it is a directory. (next if -d $file;) does not work(Gtthorne@Hotmail.com).
opendir (DIR, $srcname) || die "can't open dir $srcname: $! \n"; while (defined($file = readdir(DIR))) {next if $file =~ /^\.\.?$/; . . . }

Replies are listed 'Best First'.
Re: Reading Files/Directories
by jZed (Prior) on Apr 04, 2005 at 20:08 UTC
    next if -d $file
      This does not work! It's as if the statement is ignored, I still continue to try to process the sub-directories. The files are in a directory off of the root like c:\myfiles. There are subdirectories in this directory that are being picked up as if they are files. I need to skip the subdirectories. -d does not work!
        Did you turn the result of readdir (which is just the name within the selected directory) into a path by reattaching the directory name in front? If not, you're gonna get an odd response.

        -- Randal L. Schwartz, Perl hacker
        Be sure to read my standard disclaimer if this is a reply.

        This works for me:
        #!perl -w use strict; opendir (DIR, './') || die "can't open dir:: $! \n"; for my $file(readdir(DIR)) { next if -d $file; print $file."\n"; }