![Perl 脚本批处理等效项](https://linux22.com/image/1585923/Perl%20%E8%84%9A%E6%9C%AC%E6%89%B9%E5%A4%84%E7%90%86%E7%AD%89%E6%95%88%E9%A1%B9.png)
我创建了一个 PERL 文件,但由于它过于简单,将 16MB 的 PERL 文件通过 USB 发送给人们只是为了运行一个程序,这似乎有点“小题大做”,所以我想知道一个较小的 DOS 批处理文件是否可以工作。(我知道最重要的部分 -EXIFTOOL - 可以从 CMD 运行。(我将匿名包含代码)
这是时间 txt 文件:
ABC|10:15
DEF|10:30
XYZ|10:40
脚本加载照片,按数字顺序排列(而不是基本排序给出的 1、10、2、20),并使用 EXIF 查找照片的拍摄时间。它会从文件中取出前两个值。“如果时间 > A && 时间 < B,则移动到文件夹”因此时间戳 10:16、10:20、10:25 被移动到 ABC。
但是,10:33 时间戳高于 B...因此它将 B 设为新的 A,并从文本文件中获取下一个时间(10:40)因此现在它将 10:30 和 10:40 之间的任何时间戳移动到文件夹 DEF。(我在时间文件的末尾添加了一个虚拟条目,因此 X+1 不会尝试读取文件末尾之后的内容。循环设置为 @data-1。因此,如果文件长度为 12 行,它将循环到 11,并从末尾的虚拟行读取 x+1...这是现在,并且没有照片会有该时间戳)
您是否认为这些循环/读取目录/if 语句可以压缩为一个简单的批处理文件?
#!/usr/bin/perl
use v5.10;
use strict;
use warnings;
my $photoFolder='../../EventsSoft/photos';
use Image::ExifTool;
use File::Copy;
use Time::Local;
### IMPORTANT ###
my $mainFolder='AHS_school';
### IMPORTANT ###
if (!defined $mainFolder || $mainFolder eq ''){
mkdir $photoFolder.'/temp';
$mainFolder='temp';
}
elsif (not -e $photoFolder.'/'.$mainFolder){
mkdir $photoFolder.'/'.$mainFolder;
}
my $exif=new Image::ExifTool;
my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst,$currentFile,$nextFile,$barcode,$dummy);
my @timePic;
my @checkPics;
opendir my $ht, $photoFolder or die "Could not open photo folder for reading '$!'\n";
my @ht = grep {/\.jpg$/} readdir $ht; # Only keep the JPG files
closedir $ht;
@checkPics = map { $_->[0] } # Sorts the files so run 1,2,3 not 1,11,12
sort { $a->[1] <=> $b->[1] }
map { [$_, $_=~/(\d+)/] } @ht;
for (my $x=0; $x<@checkPics; $x++){
$exif->ExtractInfo($photoFolder.'/'.'photo'.$x.'.jpg');
$timePic[$x]=$exif->GetValue('CreateDate');
}
open (my $fh, '<', $photoFolder.'/photog1.dat') or die "Could not open photog1.dat for reading '$!'\n";
my @data=<$fh>;
close($fh);
chomp @data;
($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime(time);
if ($mon eq 11){
$mon=0;
}
else{
$mon++;
}
my $str = sprintf ("%02d:%02d %02d:%02d:%02d", $mon, $mday, $hour, $min, $sec);
my $fakeTime='XXXXXX|'.($year+1900).':'.$str;
push (@data,$fakeTime); # Saves time in ISO format
# @data holds the time barcode scanned in the format XXXAGK|2019-03-15 11:14:00
# @checkPics holds the image filename
# @timePic holds time photo was taken
# The fake entry stops reading beyond end of file with ($y+1) option below. Np photos would have been taken at this time/date
for (my $x=0; $x<@data-1; $x++){
($barcode,$currentFile)=split('\|',$data[$x]); # Gets the top value off data, and gets the barcode
($dummy,$nextFile)=split('\|',$data[($x+1)]); # Gets the next value down - files to be below this value
for (my $y=0;$y<@timePic; $y++){
if ($timePic[$y] eq 0){
next;
}
if ($timePic[$y] lt $nextFile){
mkdir $photoFolder.'/'.$mainFolder.'/'.$barcode;
copy ($photoFolder.'/'.$checkPics[$y], $photoFolder.'/'.$mainFolder.'/'.$barcode.'/'.$checkPics[$y]) or die "Copy failed line 70 '$!'\n";
$timePic[$y]=0;
}
if ($timePic[$y] gt $nextFile){
last;
}
}
}
## Because the above loop is @data-1; we do not get to the last entry, so need one final loop
my $picEnd=@data;
($barcode,$dummy)=split('\|',$data[$picEnd-2]); # Gets the final barcode
for (my $y=0;$y<@timePic; $y++){
if ($timePic[$y] eq 0){
next;
}
mkdir $photoFolder.'/'.$mainFolder.'/'.$barcode;
copy ($photoFolder.'/'.$checkPics[$y], $photoFolder.'/'.$mainFolder.'/'.$barcode.'/'.$checkPics[$y]) or die "Copy failed line 86'$!'\n";
}
if ($mainFolder eq 'temp'){
rename $photoFolder.'/temp',$photoFolder.'/!! TEMP !!';
}
say "All photos have been moved to their respective folders";