Skip to content

Instantly share code, notes, and snippets.

@kizashi1122
Created May 21, 2016 09:02
Show Gist options
  • Save kizashi1122/e077ee1671ffeaae5d9f0b337257afc5 to your computer and use it in GitHub Desktop.
Save kizashi1122/e077ee1671ffeaae5d9f0b337257afc5 to your computer and use it in GitHub Desktop.
タイムスタンプをファイル名にリネームするスクリプト(同じタイムスタンプで元のファイル名が異なる場合を想定し、連番をつける)
#!/usr/bin/env perl
use warnings;
use strict;
use feature 'say';
use Cwd;
my $dir = shift || Cwd::getcwd();
say $dir;
my $count = 0;
rename_emls($dir);
sub rename_emls {
my $dir = shift;
opendir my $dh, $dir or die "$!";
for my $subf (grep !/^\.\.?/, readdir($dh)) {
my $f = "$dir/$subf";
if (-d $f) {
rename_emls($f);
next;
}
if ($f =~ /\.eml$/) {
my $org_name = $f;
my @stat = stat($org_name);
my ($sec, $min, $hour, $day, $mon, $year) = localtime($stat[9]);
my $renamed_name = sprintf("$dir/%04d%02d%02d-%02d%02d%02d-%08d.eml", $year + 1900, $mon + 1, $day, $hour, $min, $sec, ++$count);
# say "$org_name => $renamed_name";
rename $org_name, $renamed_name if ($org_name ne $renamed_name);
}
}
closedir $dh;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment