My code displays all files within the directory, But I need it not to display hidden files such as "." and "..".

opendir(D, "/var/spool/postfix/hold/") || die "Can't open directory: $!\n";
    while (my $f = readdir(D)) 
    {
       print "MailID :$f\n";
    }
     closedir(D);
有帮助吗?

解决方案 3

On a Linux system, "hidden" files and folders are those starting with a dot.

It is best to use lexical directory handles (and file handles).

It is also important to always use strict and use warnings at the start of every Perl program you write.

This short program uses a regular expression to check whether each name starts with a dot.

use strict;
use warnings;

opendir my $dh, '/var/spool/postfix/hold' or die "Can't open directory: $!\n";

while ( my $node = readdir($dh) ) {
  next if $node =~ /^\./;
  print "MailID: $node\n";
}

其他提示

It sounds as though you might be wanting to use the glob function rather than readdir:

while (my $f = </var/spool/postfix/hold/*>) {
    print "MailID: $f\n";
}

<...> is an alternate way of globbing, you can also just use the function directly:

while (my $f = glob "/var/spool/postfix/hold/*") {

This will automatically skip the hidden files.

Just skip the files you don't want to see:

while (my $f = readdir(D)) 
  {
     next if $f eq '.' or $f eq '..';
     print "MailID :$f\n";
  }
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top