2005/08/03(水) [n年前の日記]
#5 [prog][digital] Perlでタイムスタンプ変更スクリプトを作成してみたり
あちらこちらからコピペして作成。
カレントフォルダ中の .bmp ファイルに対して、更新日時の『日付部分のみ』を、指定した日付に変更。「perl tmstchg.pl 2005 08 03」みたいな感じで使う。
ついでに、あふから呼べるようにしたり。下のようなテキストファイルを作成。
カレントフォルダ中の .bmp ファイルに対して、更新日時の『日付部分のみ』を、指定した日付に変更。「perl tmstchg.pl 2005 08 03」みたいな感じで使う。
#!/usr/bin/perl
# Last updated: <2005/08/03 22:21:13 +0900>
#
# tmstchg.pl - タイムスタンプ変更スクリプト。日付のみ変更する
#
# usage : perl tmstchg.pl YYYY MM DD
use strict;
use POSIX 'strftime';
my $ext = "bmp";
my ($now_sec,$now_min,$now_hour,
$mday_new,$mon_new,$year_new,
$now_wday,$now_yday,$now_isdst) = localtime(time);
$year_new += 1900;
$mon_new += 1;
my $listonly = 0;
if ( $#ARGV != 2 ) {
usage();
$listonly = 1;
} else {
my $yy = shift @ARGV;
my $mm = shift @ARGV;
my $dd = shift @ARGV;
if ( $yy < 2005 || $mm < 1 || $mm > 12 || $dd < 1 || $dd > 31 ) {
usage();
$listonly = 1;
} else {
$year_new = $yy;
$mon_new = $mm;
$mday_new = $dd;
}
}
my @files = glob("*.$ext");
foreach my $file (@files) {
my ($dev, $ino, $mode, $nlink, $uid, $gid, $rdev, $size,
$atime, $mtime, $ctime, $blksize, $blocks) = stat $file;
my $atimestr = strftime "%Y/%m/%d %H:%M:%S", localtime $atime;
my $mtimestr = strftime "%Y/%m/%d %H:%M:%S", localtime $mtime;
my ($sec, $min, $hour, $mday, $mon, $year, $wday, $yday, $isdst) = localtime $mtime;
my $mmtime = get_time($year_new,$mon_new,$mday_new,$hour,$min,$sec);
my $mmtimestr = strftime "%Y/%m/%d %H:%M:%S", localtime $mmtime;
print "$file\t$atimestr\t$mtimestr";
if ( $listonly == 0 ) {
print "\t->\t$mmtimestr";
utime($atime, $mmtime, $file);
}
print "\n";
}
print "\npush any key";
my $c = getc;
exit;
sub get_time {
my($yy,$mm,$mday,$hour,$min,$sec) = @_;
my($time, $year, $mon, $tz, $uruu, @mdays);
$year = $yy - 1900;
$mon = $mm - 1;
$tz = -9;
@mdays = ( 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334);
$uruu = int(($year - 72) / 4);
if (((($year + 1900) % 4) == 0) && ($mon < 2)) {
$uruu--;
}
$time = $year - 70;
$time = ($time * 365) + $mdays[$mon] + $mday + $uruu;
$time = ($time * 24) + $hour + $tz;
$time = ($time * 60) + $min;
$time = ($time * 60) + $sec;
return $time;
}
sub usage {
print "usage : perl tmstchg.pl YYYY MM DD\n\n";
}
無駄なところがありすぎだけど。まあ、勉強も兼ねて。ついでに、あふから呼べるようにしたり。下のようなテキストファイルを作成。
afx Perl Script MENU "t タイムスタンプ(日付のみ)変更" cmd.exe /c perl x:\xxxx\tmstchg.pl $I"Year (ex. 2005)" $I"Month (ex. 07)" $I"Day (ex. 03)"やってることは、「年」「月」「日」の入力ウインドウを出して、それをperlスクリプトに渡す、といった感じ。更に、ファンクションキーに以下のような感じでコマンドを割り当て。
&menu x:\xxxx\menu_perlscript.txt
[ ツッコむ ]
以上です。