指定したフォルダ内の全ての画像に対し、ファイル名を元にしてサブフォルダを自動作成し、そのフォルダに自動で割り振るためのMacroです。
スキャンした本をまとめて一気に、それぞれのフォルダに自動で収納することが出来ます。
1種類だけでなく、複数のタイトルが混ざっていてもOKです。
//MoveFiles_mkDirAuto.txt
//MoveFiles (No JPEG Compression by Re-Saving)
//MakeDirectoryAuto
//preProcess FileFormat:[trueTitle_number]
//for any pureTitle (mix together OK)
//Do something for selected folder
showMessage("Select Processing Folder");
openDir=getDirectory("Choose a Directory");
print("Processing :",openDir);
selectWindow("Log");
showMessage("Select Save Folder");
saveDir=getDirectory("Choose a Directory");
print("Save to :",saveDir);
selectWindow("Log");
list=getFileList(openDir);
wait(1000);
//make parentDirectory
parentDir=saveDir+"postProc_"+getTimeStamp()+"/";
File.makeDirectory(parentDir);
//checkFileCount(pre)
preCount = list.length;
//operation
startTime=whatTimeNow();
for (i=0; i<list.length;i++){
//Progress.the second decimal place by 10000/100
print(i+1,"/",list.length,"...Progress=",floor((i+1)/list.length*10000)/100,"%");
name=list[i];
dotIndex=lastIndexOf(name,".");
title=substring(name,0,dotIndex);
underBar=lastIndexOf(title,"_");
trueTitle=substring(title,0,underBar);
//number=substring(title,underBar+1);
//underBar2=lastIndexOf(trueTitle,"_");
//volume=substring(trueTitle,underBar2+1);
//pureTitle=substring(trueTitle,0,underBar2);
//Move to SubDirectory
subDir=parentDir+trueTitle+"/";
if(!File.exists(subDir)){
File.makeDirectory(subDir);
}
File.copy(openDir+name,subDir+name);
print("Move to...",subDir+name);
}
//checkFileCount(post)
var totalFiles = 0;
countFiles(parentDir);
postCount = totalFiles;
if(preCount != postCount) exit("Error! File has disappeared");
//fin
finishTime=whatTimeNow();
print("Start Time.... ",startTime);
print("FinishTime... ",finishTime);
print("File Count =",postCount,"...verified");
print("oshimai");
//-----------------------------------------------------------------------------
//Define function getTimeStamp
function getTimeStamp(){
getDateAndTime(year,month,dayOfWeek,dayOfMonth,hour,minute,second,msec);
timeStamp="string";
strYear=""+year;
month=month+1;
if(month<10){
strMonth="0"+month;
}else{
strMonth=""+month;
}
if(dayOfMonth<10){
strDayOfMonth="0"+dayOfMonth;
}else{
strDayOfMonth=""+dayOfMonth;
}
if(hour<10){
strHour="0"+hour;
}else{
strHour=""+hour;
}
if(minute<10){
strMinute="0"+minute;
}else{
strMinute=""+minute;
}
if(second<10){
strSecond="0"+second;
}else{
strSecond=""+second;
}
timeStamp=strYear+strMonth+strDayOfMonth+"_"+strHour+strMinute+strSecond;
return timeStamp;
}
//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------
//Define function whatTimeNow
function whatTimeNow(){
getDateAndTime(year,month,dayOfWeek,dayOfMonth,hour,minute,second,msec);
stringTime="string";
strYear=""+year;
month=month+1;
if(month<10){
strMonth="0"+month;
}else{
strMonth=""+month;
}
if(dayOfMonth<10){
strDayOfMonth="0"+dayOfMonth;
}else{
strDayOfMonth=""+dayOfMonth;
}
if(hour<10){
strHour="0"+hour;
}else{
strHour=""+hour;
}
if(minute<10){
strMinute="0"+minute;
}else{
strMinute=""+minute;
}
if(second<10){
strSecond="0"+second;
}else{
strSecond=""+second;
}
stringTime=strYear+"/"+strMonth+"/"+strDayOfMonth+"_"+strHour+":"+strMinute+":"+strSecond;
return stringTime;
}
//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------
//Define countFiles
function countFiles(dir){
list = getFileList(dir);
for(i=0; i<list.length; i++){
if(endsWith(list[i],"/")){
countFiles(dir+list[i]);
}else{
totalFiles++;
}
}
}
//-----------------------------------------------------------------------------
コードの内容
- 作業対象となるOpen Folderと保存先のSave Folderを設定。
- フォルダ内の全データのファイル名を解析し、本のタイトル(trueTitle)、ページ(number)を取り出す。
- 『postProc_timeStamp』の形式で親フォルダを作成する。
- 親フォルダの下に『trueTitle』の名前でサブフォルダを作成する。
- それぞれの画像を正しいサブフォルダの中に移動(コピー)させる。
- 移動前のファイル数と移動後のファイル数をカウント・比較し、漏れがないか確認する。問題なければLogに「verified」と表示する。 (2020.06.21.追記)
事前準備
ファイル名を取り出すために、アンダーバーの位置を「後ろ側」から調べているので、スキャンした画像のファイル名を『trueTitle_number.拡張子』の形式で保存しておく必要があります。
(例)ONE PIECE_061_00154.jpg
(trueTitle=ONE PIECE_061, number=00154)
もし違う形式で保存している場合は、コードの中身を書き換えることでも対応できると思います。
マクロの起動方法
①ImageJ上部タブの[Plugins]→[New]→[Macro]で起動したエディタに、記事の一番上のコードをコピペしてtxtファイル(MoveFiles_mkDirAuto.txt)を作成・保存する。
②保存したファイルをImageJフォルダ内の[plugins]フォルダにしまう。
このとき、[plugins]フォルダの中に新たに適当な名前のフォルダを作って、その中にしまってもOKです。ここでは仮に「自炊」というフォルダにtxtファイルを突っ込んだとします。
③一度ImageJを再起動すると、マクロがインストールされ、起動準備OK。
④上部タブ[Plugins]→[自炊]→[MoveFiles mkDirAuto.txt]でマクロが実行されます。
注意
ImageJはPDFファイルをサポートしていないので、PDFで自炊している方はあらかじめ全てのファイルをJPGに直しておいて下さい!
Macの方はデフォルトでインストールされている「Automator」を使用するとJPGに直すことが出来ます。
ライセンスなんかは一切無いので、ぜひぜひ自由に使ってみてください!