ファイル検索
指定フォルダ以下のフォルダ/ファイルを列挙する
lesson001.pl
実行結果
$dir = $ARGV[0];
opendir HANDLE, $dir;
@fileList = readdir HANDLE;foreach $file (@fileList)
{
print "$file\n";
}closedir HANDLE, $dir;
C:\>perl c:\study\perl\chapter005\lesson001.pl c:\study\perl
.
..
chapter001
chapter002
chapter003
chapter004
chapter005
ちょいと変更
lesson002.pl
実行結果
putsDir($ARGV[0]);sub putsDir
{
my($dir) = @_;
opendir HANDLE, $dir;
my @fileList = readdir HANDLE;foreach $file (@fileList)
{
print "$file\n";
}closedir HANDLE, $dir;
}
C:\>perl c:\study\perl\chapter005\lesson002.pl c:\study\perl
.
..
chapter001
chapter002
chapter003
chapter004
chapter005
サブフォルダも検索
lesson003.pl
実行結果
putsDir($ARGV[0]);sub putsDir
{
my($path) = @_;
if (-d $path)
{
opendir HANDLE, $path;
my @fileList = readdir HANDLE;foreach $file (@fileList)
{
next if ($file eq "."); # 自分
next if ($file eq ".."); # 親putsDir("$path\\$file");
}closedir HANDLE, $path;
}
else
{
print "$path\n";
}
}
C:\>perl c:\study\perl\chapter005\lesson003.pl c:\study\perl
c:\study\perl\chapter001\lesson001.pl
c:\study\perl\chapter001\lesson002.pl
c:\study\perl\chapter001\lesson003.pl
c:\study\perl\chapter001\lesson004.pl
c:\study\perl\chapter001\lesson011.pl
c:\study\perl\chapter001\perl.bat
c:\study\perl\chapter002\lesson001.pl
c:\study\perl\chapter002\lesson002.pl
c:\study\perl\chapter002\lesson003.pl
c:\study\perl\chapter002\lesson004.pl
c:\study\perl\chapter002\lesson005.pl
c:\study\perl\chapter002\perl.bat
c:\study\perl\chapter003\lesson001.pl
c:\study\perl\chapter003\lesson002.pl
c:\study\perl\chapter003\lesson003.pl
c:\study\perl\chapter003\lesson004.pl
c:\study\perl\chapter003\lesson005.pl
c:\study\perl\chapter003\lesson006.pl
c:\study\perl\chapter003\perl.bat
c:\study\perl\chapter003\test.txt
c:\study\perl\chapter004\lesson001.pl
c:\study\perl\chapter004\lesson002.pl
c:\study\perl\chapter004\lesson004.pl
c:\study\perl\chapter004\lesson006.pl
c:\study\perl\chapter004\lesson007.pl
c:\study\perl\chapter004\lesson008.pl
c:\study\perl\chapter004\lesson009.pl
c:\study\perl\chapter004\lesson010.pl
c:\study\perl\chapter004\lesson011.pl
c:\study\perl\chapter004\perl.bat
c:\study\perl\chapter005\lesson001.pl
c:\study\perl\chapter005\lesson002.pl
c:\study\perl\chapter005\lesson003.pl
c:\study\perl\chapter005\perl.bat