ディレクトリ操作系、ファイル操作系の処理プログラムメモです。
- FIle::Path
- File::Spec
- Time::Piece
- stat
- unlink
保存ディレクトリが無い場合、作成してファイルを保存
- File::Path
- ディレクトリ作成
- FIle::Spec
- OS依存に対応したパス作成
use strict; use warnings; use File::Path qw/mkpath/; use File::Spec; my $base_dir = $ARGV[0]; my $kind = $ARGV[1]; my $save_path = File::Spec->catfile($base_dir,$kind); unless (-e $save_path) { mkpath $save_path; } my $file_path = File::Spec->catfile($save_path, 'test.txt'); open my $fh, '>', $file_path; print $fh "test.\n"; close($fh);
一定時間過ぎたファイルを削除
- Time::Piece
- 時間操作処理を行なうモジュール
- File::Spec
- stat
- ファイルの情報を取得する関数
- unlink
- ファイル削除の関数
作成されてから二週間過ぎたファイルを削除します。
use strict; use warnings; use Time::Piece qw/localtime/; use Time::Seconds; use File::Spec; my $dir = File::Spec->catfile('/','tmp'); opendir(DIR,$dir); my @files = readdir(DIR); closedir(DIR); my $t = localtime; my $ago = $t - ONE_WEEK * 2; for my $file (@files) { my $file_path = File::Spec->catfile($dir,$file); my $dt = localtime((stat($file_path))[9]); if ($ago > $dt) { unlink $file_path; } }