ブラウザからローカルのファイルを探す。後はお好きに。

# searchFiles

二つのクラスで構成されたJAVAアプリです。
本日作成された指定名のファイルをローカルマシンから探し出します。
本日固定ですが、簡単に日付指定に変更できます。
## 使い方
### **searchFiles.jar 探したいファイル名（パスではありません）**
## コード１　ExecuteShellComand.java

```java:ExecuteShellComand.java
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;

public class ExecuteShellComand {
	// http://chipmunkland.blog104.fc2.com/blog-entry-197.html
	public String[] executeCommandSpecial(String command,int _timewait){
		System.out.println("executeCommandSpecial "+command+" _timewait="+_timewait);
		if (_timewait<=0){
			_timewait = 1000 * 60 * 10;// 600,000ミリ秒=600秒=10分 60秒＝1分＝60,000ミリ秒 10000ミリ秒＝10秒
		}
	    String[] returns = new String[3];
	    InputStream in = null;
		ProcessBuilder pb = new ProcessBuilder(command);
		pb.redirectErrorStream(true); // 標準エラーを標準出力にマージ
		Process p = null;
		try {
			p = pb.start();
			long timeout = _timewait;// http://chipmunkland.blog104.fc2.com/blog-entry-197.html
			long tm = System.currentTimeMillis();
			InputStreamReader isr = new InputStreamReader(p.getInputStream());
			try {
				while (true) {
					System.out.print(">");
					if (System.currentTimeMillis() - tm > timeout) { //タイムアウトチェック
						p.destroy(); // プロセス終了
						// break;
						System.out.println("executeCommandSpecial プロセスタイムアウト");
				        return returns;
					}
					try {
						if (isr.ready()) isr.read();
					} catch (IOException e) {
						e.printStackTrace();
						System.out.println("executeCommandSpecial isr.readエラー e="+e);
					}// readyを使って出力可能か判定しながら吸い出す
					try { //終了チェック
						p.exitValue();
						break;
					} catch (IllegalThreadStateException ex) {
						System.out.println("executeCommandSpecial IllegalThreadStateException ex="+ex);
						try {
							Thread.sleep(100);
						} catch (InterruptedException e) {
							e.printStackTrace();
						}
					}
				}
			} finally {
				try {
					isr.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
	        return returns;				
		} catch (IOException e) {
			e.printStackTrace();
		}
        return returns;				
	}

	public String[] executeCommand(String command,int _waittime){
	    String[] returns = new String[3];
	    String LINE_SEPA = System.getProperty("line.separator");
		if (_waittime<=0){
			_waittime = 1000 * 60 * 10;// 600,000ミリ秒=600秒=10分
		}
		long timeout = _waittime;// http://chipmunkland.blog104.fc2.com/blog-entry-197.html
		long tm = System.currentTimeMillis();
		StringBuffer output = new StringBuffer();
		Process p = null;
	    InputStream in = null;
	    BufferedReader br = null; 
	    try {
			p = Runtime.getRuntime().exec(command);
			p.waitFor();
			BufferedReader reader = new BufferedReader(new InputStreamReader(p.getInputStream()));
            String line = "";			
			while ((line = reader.readLine())!= null) {
				output.append(line + "\n");
			}
	        returns[0] = output.toString();
	        in = p.getErrorStream();
	        StringBuffer err = new StringBuffer();
	        br = new BufferedReader(new InputStreamReader(in));
	        while ((line = br.readLine()) != null) {
	            err.append(line + LINE_SEPA);
	        }
	        returns[1] = err.toString();
	        returns[2] = Integer.toString(p.waitFor());
			if (System.currentTimeMillis() - tm > timeout) { //タイムアウトチェック
				p.destroy(); // プロセス終了
		        return returns;
			}
	        return returns;
		} catch (Exception e) {
			e.printStackTrace();
			System.out.println("Exception e="+e);
		}
        return returns;
	}
}
```

## コード2　Main.java

```java:Main.java
import java.io.File;
import java.text.SimpleDateFormat;
import java.util.Date;


public class Main {
	public static void main(String[] args) {
    	ExecuteShellComand obj = new ExecuteShellComand();
        //String command  ="mdfind -name "+fileName;//command  =pythonPath + "getFilePathMdfind.sh";

    	String fileName = "あああ.txt";
        //String _modifiedDate = "Thu Apr 07 2016 05:24:50 GMT+0900 (JST)";// stat "Aug 18 18:28:37 2015"
		if(args.length>0){
			fileName = args[0];
		}
    	
		Date date = new Date();
		String today = date.toString();
		SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy-MM-dd");
		String theDay = sdf1.format(date);
        System.out.println(today);
        //String _modifiedDate = "Thu Apr 07 2016 05:24:50 GMT+0900 (JST)";// stat "Aug 18 18:28:37 2015"
		if(args.length>0){
			fileName = args[0];
			theDay = args[1];//yyyy-MM-dd
		}

		String command = "";
		String _os = System.getProperty("os.name").toLowerCase();
		if(_os.startsWith("linux")){
			command = "locate " + fileName;
		}else if(_os.startsWith("mac")){
			//String command = "mdfind 'kMDItemFSCreationDate=2016-10-31 && kMDItemDisplayName='"+fileName+"'\"";
			//String command = "mdfind 'kMDItemDisplayName='"+fileName+"'";
			command = "mdfind kMDItemFSCreationDate >= $time.iso("+theDay+") && kMDItemDisplayName=\""+fileName+"\"";
		}else if(_os.startsWith("windows")){
	        System.out.println("未対応");
		}else if(_os.startsWith("sunos")){
	        System.out.println("未対応");
		}else{
		}
        System.out.println("command="+command);
	    
        String[] ret = obj.executeCommand(command,-1);
 	    File fromF = new File(ret[0].trim());
	    File toF = new File("/hoge/hoge/"+fileName);
 	    fromF.renameTo(toF);
	}

	public Main() {
		super();
	}
}
```

:bowtie: 今日は文化の日でした。2016年11月3日20時17分