Perl ディレクトリ内のファイル数を数える方法【サンプルコード付き】

以下のPerlスクリプトを使用して、ディレクトリ内のファイル数をカウントすることができます。

#!/usr/bin/perl

use strict;
use warnings;

my $dir = "/path/to/directory"; # 替换为您要统计的目录路径

opendir(my $dh, $dir) or die "无法打开目录: $!";
my @files = grep { -f "$dir/$_" } readdir($dh);
closedir($dh);

my $file_count = scalar @files;
print "目录 $dir 中的文件数为: $file_count\n";

指定しているディレクトリのパスを/path/to/directoryから、統計したいディレクトリのパスに置き換えて、上記のスクリプトを実行すると、そのディレクトリ内のファイル数をカウントできます。

bannerAds