سؤال

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