ファイルを読み込んで一括変換するようにしました。
��=入力テキストは以下==
2004/04/251631 22009_01_19_22_34_10.wmv
2004/04/251635 22009_01_19_22_37_42.wmv
2004/04/25163632009_01_19_22_38_35.wmv
2004/04/25163732009_01_19_22_38_57.wmv
��=
��=JAVAの処理==
上記テキストを読み込んで日付部とファイル名部にわけて
ファイル名を日付の名前に変更して、更新日時を変更する。
��=JAVAはここから==
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package filerename;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
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\\デスクトップ\\test\\");
String inputListFile = new String("test.txt");
//String BR = System.getProperty("line.separator");
//ファイルの読み込みと入力
try{
FileReader fr = new FileReader(folderpath+inputListFile);
BufferedReader br = new BufferedReader(fr);
//FileOutputStream fos = new FileOutputStream(folderpath+"rename_log.txt");
//OutputStreamWriter osw = new OutputStreamWriter(fos);
FileWriter fw = new FileWriter(folderpath+"rename_log.txt");
BufferedWriter bw = new BufferedWriter(fw);
String msg = "ファイル名変更ログメッセージの出力開始。";
bw.write(msg);
bw.newLine();
String inputString;
while((inputString = br.readLine()) != null){
System.out.println(inputString);
//String inputString = new String("2004/04/251631 2009_01_19_22_34_10.wmv");
//インプットのStringを整形
// "/"を削除
String afterReplace1 = inputString.replaceAll("/", "");
// 空白を削除
String afterReplace = afterReplace1.replaceAll(" ", "");
System.out.println("after replace string:"+afterReplace);
bw.write("after replace string:"+afterReplace);
bw.newLine();
if(afterReplace.length() < 36){
System.out.println("Input String is less than 36");
bw.write("Input String is less than 36");
bw.newLine();
}
else{
//最初から4文字目を抜き出す(年数部分)
String Syear = new String( afterReplace.substring( 0, 4 ) );
//5,6文字目を抜き出す(月部分)
String Smonth = new String( afterReplace.substring( 4, 6 ) );
//7,8文字目を抜き出す(日数部分)
String Sday = new String( afterReplace.substring( 6, 8 ) );
//9,10を抜き出す(時間部分)
String Shour = new String( afterReplace.substring( 8, 10 ) );
//11,12を抜き出す(分部分)
String Smin = new String( afterReplace.substring( 10, 12 ) );
//残りを抜き出す(元ファイル名)
String orgname = new String( afterReplace.substring( 13, 36 ) );
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);
//StringをInegerへ変換
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) ;
System.out.println("元ファイル名:"+orgname);
bw.write("元ファイル名:"+orgname);
bw.newLine();
//変更前ファイル名
File orgFile = new File(folderpath + orgname);
boolean orgfileext = orgFile.exists();
System.out.println(orgFile + (orgfileext? " is found " : " is missing "));
//変更後のファイル名
StringBuffer filename = new StringBuffer("Z5_"+Syear+"_"+Smonth+"_"+Sday+"_"+Shour+"_"+Smin);
//変更後のファイル名がすでにないか確認して変更
System.out.println("Rename candidate is "+filename+".wmv");
while((new File(folderpath + filename+".wmv")).exists()){
System.out.println(folderpath + filename +" exists");
min = min + 1;
filename.replace(14,16, String.valueOf(min) );
}
File newFile = new File(folderpath + filename+".wmv");
if(orgfileext){
System.out.println("Start Rename to "+newFile.getName()+" from "+orgFile.getName());
bw.write("Start Rename to "+newFile.getName()+" from "+orgFile.getName());
bw.newLine();
boolean rename = orgFile.renameTo(newFile);
if(rename){
//ファイル名変更成功
System.out.println("Rename Successful");
bw.write("Rename Successful");
bw.newLine();
//日付を設定 (月は設定したい月-1が必要)
Calendar newcalendar = Calendar.getInstance();
newcalendar.set(year, month-1, day, hour, min, 00);
System.out.println(newcalendar.getTime());
//更新日付を変更
boolean lastTime=newFile.setLastModified(newcalendar.getTimeInMillis());
System.out.println("Last Modified: "+lastTime);
System.out.println(newFile.lastModified());
}
else{
//ファイル名変更失敗
System.out.println("Fail to rename");
bw.write("Fail to rename");
bw.newLine();
}
}
else{
boolean rename = false;
System.out.println(orgFile +" not exists.");
bw.write(orgFile +" not exists.");
bw.newLine();
}
}
}
br.close();
fr.close();
bw.close();
fw.close();
System.out.println("");
System.out.println("読み込み終了");
}
catch(IOException e){
e.printStackTrace();
System.out.println("ファイル入出力で不正な処理が発生しました.");
System.exit(1);
}
}
}
��=入力テキストは以下==
2004/04/251631 22009_01_19_22_34_10.wmv
2004/04/251635 22009_01_19_22_37_42.wmv
2004/04/25163632009_01_19_22_38_35.wmv
2004/04/25163732009_01_19_22_38_57.wmv
��=
��=JAVAの処理==
上記テキストを読み込んで日付部とファイル名部にわけて
ファイル名を日付の名前に変更して、更新日時を変更する。
��=JAVAはここから==
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package filerename;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
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\\デスクトップ\\test\\");
String inputListFile = new String("test.txt");
//String BR = System.getProperty("line.separator");
//ファイルの読み込みと入力
try{
FileReader fr = new FileReader(folderpath+inputListFile);
BufferedReader br = new BufferedReader(fr);
//FileOutputStream fos = new FileOutputStream(folderpath+"rename_log.txt");
//OutputStreamWriter osw = new OutputStreamWriter(fos);
FileWriter fw = new FileWriter(folderpath+"rename_log.txt");
BufferedWriter bw = new BufferedWriter(fw);
String msg = "ファイル名変更ログメッセージの出力開始。";
bw.write(msg);
bw.newLine();
String inputString;
while((inputString = br.readLine()) != null){
System.out.println(inputString);
//String inputString = new String("2004/04/251631 2009_01_19_22_34_10.wmv");
//インプットのStringを整形
// "/"を削除
String afterReplace1 = inputString.replaceAll("/", "");
// 空白を削除
String afterReplace = afterReplace1.replaceAll(" ", "");
System.out.println("after replace string:"+afterReplace);
bw.write("after replace string:"+afterReplace);
bw.newLine();
if(afterReplace.length() < 36){
System.out.println("Input String is less than 36");
bw.write("Input String is less than 36");
bw.newLine();
}
else{
//最初から4文字目を抜き出す(年数部分)
String Syear = new String( afterReplace.substring( 0, 4 ) );
//5,6文字目を抜き出す(月部分)
String Smonth = new String( afterReplace.substring( 4, 6 ) );
//7,8文字目を抜き出す(日数部分)
String Sday = new String( afterReplace.substring( 6, 8 ) );
//9,10を抜き出す(時間部分)
String Shour = new String( afterReplace.substring( 8, 10 ) );
//11,12を抜き出す(分部分)
String Smin = new String( afterReplace.substring( 10, 12 ) );
//残りを抜き出す(元ファイル名)
String orgname = new String( afterReplace.substring( 13, 36 ) );
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);
//StringをInegerへ変換
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) ;
System.out.println("元ファイル名:"+orgname);
bw.write("元ファイル名:"+orgname);
bw.newLine();
//変更前ファイル名
File orgFile = new File(folderpath + orgname);
boolean orgfileext = orgFile.exists();
System.out.println(orgFile + (orgfileext? " is found " : " is missing "));
//変更後のファイル名
StringBuffer filename = new StringBuffer("Z5_"+Syear+"_"+Smonth+"_"+Sday+"_"+Shour+"_"+Smin);
//変更後のファイル名がすでにないか確認して変更
System.out.println("Rename candidate is "+filename+".wmv");
while((new File(folderpath + filename+".wmv")).exists()){
System.out.println(folderpath + filename +" exists");
min = min + 1;
filename.replace(14,16, String.valueOf(min) );
}
File newFile = new File(folderpath + filename+".wmv");
if(orgfileext){
System.out.println("Start Rename to "+newFile.getName()+" from "+orgFile.getName());
bw.write("Start Rename to "+newFile.getName()+" from "+orgFile.getName());
bw.newLine();
boolean rename = orgFile.renameTo(newFile);
if(rename){
//ファイル名変更成功
System.out.println("Rename Successful");
bw.write("Rename Successful");
bw.newLine();
//日付を設定 (月は設定したい月-1が必要)
Calendar newcalendar = Calendar.getInstance();
newcalendar.set(year, month-1, day, hour, min, 00);
System.out.println(newcalendar.getTime());
//更新日付を変更
boolean lastTime=newFile.setLastModified(newcalendar.getTimeInMillis());
System.out.println("Last Modified: "+lastTime);
System.out.println(newFile.lastModified());
}
else{
//ファイル名変更失敗
System.out.println("Fail to rename");
bw.write("Fail to rename");
bw.newLine();
}
}
else{
boolean rename = false;
System.out.println(orgFile +" not exists.");
bw.write(orgFile +" not exists.");
bw.newLine();
}
}
}
br.close();
fr.close();
bw.close();
fw.close();
System.out.println("");
System.out.println("読み込み終了");
}
catch(IOException e){
e.printStackTrace();
System.out.println("ファイル入出力で不正な処理が発生しました.");
System.exit(1);
}
}
}
コメント