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


Hi Monks,

I need to skip the files or directories that are not present in the directory.

I am reading a text file which contains directory names.

I am reading the contents of each directory in the file.

I want to print a warning message in the screen for the directories which are not present and continue processing other directories.

For example if the text file contains the following directories

DIR1,DIR2,DIR3,DIR4,DIR5;

If the directories DIR2 and DIR3 are not there, I just want to print a warning statement and conitnue processing the other directories.

I am using the following code

I am trying to use "continue" flow control statement
opendir DIR,$dir or continue{print"[WARNING]$dir not present\n"};
Can any one help me on this?

Thanks in advance

Replies are listed 'Best First'.
Re: Skipping files in a directory
by ccn (Vicar) on Dec 11, 2008 at 10:51 UTC
    perldoc -X
    foreach my $dir (@DIRS) { warn "Directory $dir not found!\n" and next unless -d $dir; # # work with existing directory here # }
      ++ccn as it is obviously much better to check for the existence of a directory before trying to open it and warn on failure
        as it is obviously much better to check for the existence of a directory before trying to open it and warn on failure

        That might be OK here, but in the general case it is not, because of race conditions. If you check for the existence of a directory, and then open it, it might be deleted by another process between these two operations.

        Thus you have to do the error checking anyway, and don't gain anything by another call to stat (which most file test ops do).

        That's why the general philosophy with file access and IO is "try and see if it worked", not "first test if it might work, and then try".


        Thanks a lot svenXY and ccn
Re: Skipping files in a directory
by svenXY (Deacon) on Dec 11, 2008 at 10:53 UTC
    Hi,
    if (opendir DIR,$dir) { # do stuff with contents } else { print "[WARNING] $dir not present\n"; }

    This will work without continue. continue works with loops like for, foreach, while, but - AFAIK - not with a simple opendir statement
  • Regards,
    svenXY
Re: Skipping files in a directory
by brsaravan (Scribe) on Dec 11, 2008 at 11:16 UTC
    While working on files or directories, use File Test Operators, to test different aspects of a file.
    open(FH, "txtfile") or die "Cannot open the file"; while (<FH>) { chomp($_); if (-d $_) { ---do your stuff here } else { print "Directory does not exist"; } } close(FH);
Re: Skipping files in a directory
by dHarry (Abbot) on Dec 11, 2008 at 11:16 UTC

    I often use File::Find.

    Something like:

    use File::Find; find(\&wanted, @directories_to_search); sub wanted { ... }

    In the wanted sub you can do the verifications you need.

Re: Skipping files in a directory
by lakshmananindia (Chaplain) on Dec 11, 2008 at 12:02 UTC

    You can also use the warn to print the warnings

    my @DIRS=qw(there notthere there); foreach (@DIRS) { if (-d $_) { # Do the task. } else { warn "File not found\n"; } }