Perl - ディレクトリ内のファイルを最終変更時刻でソートして連番をつけてリネームする
- ディレクトリ内のファイルを作成時間の昇順ルールで添え字をつけてリネームするスクリプト - Kshi_Kshi's blog
- ディレクトリ内のファイルを作成時刻でソートして連番をつけてリネームする - Study08.net 対シンバシ殲滅用人型機動兵器
を Perl でやってみた。Python がわからないのでやってることが同じか確証がもてません。
#!/usr/bin/env perl use strict; use warnings; use File::Basename; my $dir_path = "/Users/hogehoge/hogehoge"; my $rename_pattern = "IMG_%05d%s"; die "ERROR: '$dir_path' is not directory.\n" unless -d $dir_path; chdir $dir_path; my @files = map $_->[0], sort {$b->[1] <=> $a->[1]} map [$_, -M], grep -f, glob '*' ; for my $index (0 .. $#files) { my $file = $files[$index]; my $ext = (fileparse($file, '\.[^.]*'))[2]; rename $file, sprintf($rename_pattern, $index+1, $ext); }
今日の疑問
- Perl でファイルの作成時刻を取得するには?
- ファイルテスト -C は inode change time in seconds since the epoch らしい。これの注意書きで、Not all fields are supported on all filesystem types. Notably, the ctime field is non-portable. In particular, you cannot expect it to be a "creation time"; see "Files and Filesystems" in perlport for details. と書いてある。 "Files and Filesystems" を後で読む。
- ファイル取得には glob, <*>, readdir のどれを使うべきなのか?