OCRソフトを通して実際に得られる文字列を
2004/04/25 16 31 2009_01_19_22_34_10.wmv
��日付、分、秒、元のファイル名)
というように想定し、この文字列を元に日付を取得し、変換するスクリプトに修正しました。
結局splitコマンドは使わずにIndexから文字列を抽出。
��参考URL http://ameblo.jp/archive-redo-blog/entry-10034128031.html)
また余計な文字列の削除にはreplaceAllを使用しました。
次回はファイルのリストを読み込んで一括処理するようにします。
それともう少し条件分岐のところを綺麗にするか。。。
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package filerename;
import java.io.File;
import java.util.Calendar;
/**
*
* @author nao
*/
public class Main {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here
//setup initial variable
String folderpath = new String("C:\\Documents and Settings\\nao\\デスクトップ\\");
String inputString = new String("2004/04/251631 2009_01_19_22_34_10.wmv");
//Modify inputString
String afterReplace1 = inputString.replaceAll("/", "");
String afterReplace = afterReplace1.replaceAll(" ", "");
System.out.println("after replace string:"+afterReplace);
String Syear = new String( afterReplace.substring( 0, 4 ) );
String Smonth = new String( afterReplace.substring( 4, 6 ) );
String Sday = new String( afterReplace.substring( 6, 8 ) );
String Shour = new String( afterReplace.substring( 8, 10 ) );
String Smin = new String( afterReplace.substring( 10, 12 ) );
String orgname = new String( afterReplace.substring( 12, 35 ) );
System.out.println("Year:"+Syear);
System.out.println("Month:"+Smonth);
System.out.println("Day:"+Sday);
System.out.println("Hour:"+Shour);
System.out.println("Min:"+Smin);
System.out.println("元ファイル名:"+orgname);
//変更前ファイル名
File orgFile = new File(folderpath + orgname);
System.out.println(orgFile + (orgFile.exists()? " is found " : " is missing "));
//変更後のファイル名
String filename = new String(Syear+"_"+Smonth+"_"+Sday+"_"+Shour+"_"+Smin);
//変更後のファイル名がすでにないか確認して変更
File newFile = new File(folderpath + filename+".wmv");
boolean ext = newFile.exists();
if(ext){
System.out.println(newFile +" exists");
}
else{
System.out.println(orgFile +" not exists.");
System.out.println("Start Rename to "+newFile);
if(orgFile.renameTo(newFile)){
//ファイル名変更成功
System.out.println("Rename Successful");
}
else{
//ファイル名変更失敗
System.out.println("Fail to rename");
}
}
//日付を設定 (月は設定したい月-1が必要)
int year = Integer.parseInt(Syear);
int month = Integer.parseInt(Smonth);
int day = Integer.parseInt(Sday);
int hour = Integer.parseInt(Shour);
int min = Integer.parseInt(Smin);
Calendar newcalendar = Calendar.getInstance();
newcalendar.set(year, month-1, day, hour, min, 00);
System.out.println(newcalendar.getTime());
// System.out.println(newcalendar.getTimeInMillis());
//System.out.println(newFile.lastModified());
//更新日付を変更
boolean lastTime=newFile.setLastModified(newcalendar.getTimeInMillis());
System.out.println("Last Modified: "+lastTime);
System.out.println(newFile.lastModified());
}
}
2004/04/25 16 31 2009_01_19_22_34_10.wmv
��日付、分、秒、元のファイル名)
というように想定し、この文字列を元に日付を取得し、変換するスクリプトに修正しました。
結局splitコマンドは使わずにIndexから文字列を抽出。
��参考URL http://ameblo.jp/archive-redo-blog/entry-10034128031.html)
また余計な文字列の削除にはreplaceAllを使用しました。
次回はファイルのリストを読み込んで一括処理するようにします。
それともう少し条件分岐のところを綺麗にするか。。。
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package filerename;
import java.io.File;
import java.util.Calendar;
/**
*
* @author nao
*/
public class Main {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here
//setup initial variable
String folderpath = new String("C:\\Documents and Settings\\nao\\デスクトップ\\");
String inputString = new String("2004/04/251631 2009_01_19_22_34_10.wmv");
//Modify inputString
String afterReplace1 = inputString.replaceAll("/", "");
String afterReplace = afterReplace1.replaceAll(" ", "");
System.out.println("after replace string:"+afterReplace);
String Syear = new String( afterReplace.substring( 0, 4 ) );
String Smonth = new String( afterReplace.substring( 4, 6 ) );
String Sday = new String( afterReplace.substring( 6, 8 ) );
String Shour = new String( afterReplace.substring( 8, 10 ) );
String Smin = new String( afterReplace.substring( 10, 12 ) );
String orgname = new String( afterReplace.substring( 12, 35 ) );
System.out.println("Year:"+Syear);
System.out.println("Month:"+Smonth);
System.out.println("Day:"+Sday);
System.out.println("Hour:"+Shour);
System.out.println("Min:"+Smin);
System.out.println("元ファイル名:"+orgname);
//変更前ファイル名
File orgFile = new File(folderpath + orgname);
System.out.println(orgFile + (orgFile.exists()? " is found " : " is missing "));
//変更後のファイル名
String filename = new String(Syear+"_"+Smonth+"_"+Sday+"_"+Shour+"_"+Smin);
//変更後のファイル名がすでにないか確認して変更
File newFile = new File(folderpath + filename+".wmv");
boolean ext = newFile.exists();
if(ext){
System.out.println(newFile +" exists");
}
else{
System.out.println(orgFile +" not exists.");
System.out.println("Start Rename to "+newFile);
if(orgFile.renameTo(newFile)){
//ファイル名変更成功
System.out.println("Rename Successful");
}
else{
//ファイル名変更失敗
System.out.println("Fail to rename");
}
}
//日付を設定 (月は設定したい月-1が必要)
int year = Integer.parseInt(Syear);
int month = Integer.parseInt(Smonth);
int day = Integer.parseInt(Sday);
int hour = Integer.parseInt(Shour);
int min = Integer.parseInt(Smin);
Calendar newcalendar = Calendar.getInstance();
newcalendar.set(year, month-1, day, hour, min, 00);
System.out.println(newcalendar.getTime());
// System.out.println(newcalendar.getTimeInMillis());
//System.out.println(newFile.lastModified());
//更新日付を変更
boolean lastTime=newFile.setLastModified(newcalendar.getTimeInMillis());
System.out.println("Last Modified: "+lastTime);
System.out.println(newFile.lastModified());
}
}
コメント