ディレクトリに拡張子が付いたファイルがあるかどうかを確認する方法を知りたいです.txt
。
どのようにそれを行うことができますか?
単にグロブを使用する:
if ( <*.txt> ) { ... }
*.txt
実際にはディレクトリである可能性があることに注意してください。それがあなたが求めているファイルである場合grep
、glob
if ( grep -f, glob '*.txt' ) { ... }
#! /usr/bin/perl
my $dir = '.'; # current dir
opendir(DIRHANDLE, $dir) || die "Couldn't open directory $dir: $!\n";
my @txt = grep { /\.txt$/ && -f "$dir/$_" } readdir DIRHANDLE;
closedir DIRHANDLE;
print ".txt files found" if (scalar(@txt));
おまけとして、見つかったすべての .txt ファイルは配列 @txt にあります。