DOS 关闭文件夹
package com.cczhang.jsabms;
import com.cczhang.utils.EncodingUtil;
import com.csvreader.CsvReader;
import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.*;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
public class CheckBackFiles {
static String WindowsTitle = "";
static Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
static String filePath = "C:\\Users\\zhangcongcong\\Desktop\\1.csv";
static String overPath = "C:\\Users\\zhangcongcong\\Desktop\\test.csv";
static String screenPath = "C:\\soft\\screen\\";
public static void main(String[] args) throws IOException, InterruptedException, AWTException {
System.out.println("Java -jar CheckBackFiles.jar 源文件路径 最后生成的csv文件路径 截屏路径");
if(args.length > 2){
filePath = args[0];
overPath = args[1];
screenPath = args[2];
}
Robot robot = new Robot();
File file = new File(filePath);
File batdir = new File(file.getParent() +File.separatorChar+ "bat");
batdir.mkdirs();
List<String[]> allPath = resolveFile(file);
for(int i = 1 ; i < allPath.size() ; i ++){
String[] filePath = allPath.get(i);
String[] newPathInfo = Arrays.copyOf(filePath,filePath.length +4);
for(int m = 3 ; m < filePath.length - 2 ; m ++){
Runtime runtime = Runtime.getRuntime();
if(!"".equals(filePath[m])){
if(new File(filePath[m]).exists()){
runtime.exec("cmd /c start "+filePath[m]+"");
WindowsTitle = filePath[m].substring(filePath[m].lastIndexOf("/")+1,filePath[m].length());
Thread.sleep(2000);
String fileName = screen_shot(robot,new File(screenPath+WindowsTitle+".png"));
newPathInfo[filePath.length + m - 3] = "file://"+screenPath+fileName;
Thread.sleep(2000);
String windowsCloseBat = "mshta \"javascript:wnds=new ActiveXObject('Shell.Application').windows(); for (i=0; i<wnds.Count; i++) {w=wnds(i); if(w) if(w.document) if(w.document.folder) if('"+WindowsTitle+"'==w.document.folder.title) w.quit();} close();\""+
"\r\n" + "exit";
String batName = batdir.getAbsolutePath()+"\\"+WindowsTitle + ".bat";
WriteFile(new File(batName),windowsCloseBat);
runtime.exec("cmd /c start "+batName+"");
}else{
System.out.println(filePath[m] + "不存在!");
newPathInfo[filePath.length + m - 3] = "路径不存在";
new File(filePath[m]).mkdirs();
continue;
}
}
}
System.out.println("完成"+(i) + "/" + (allPath.size() - 1));
allPath.set(i,newPathInfo);
}
String newContent = "";
for(int i = 0 ; i < allPath.size() ; i ++){
for(String line : allPath.get(i)){
newContent += line + ",";
}
newContent += "\r\n";
}
WriteFile(new File(overPath),newContent);
}
public static void WriteFile(File _file,String content){
try {
BufferedWriter bw = new BufferedWriter(new FileWriter(_file));
bw.write(content);
bw.newLine();
bw.close();
} catch (IOException e) {
e.printStackTrace();
}
}
public static String screen_shot(Robot robot,File screenFile) {
Rectangle screenRect = new Rectangle(0, 0, screenSize.width, screenSize.height);
BufferedImage bufferedImage = robot.createScreenCapture(screenRect);
try {
ImageIO.write(bufferedImage, "png", screenFile);
} catch (IOException e) {
e.printStackTrace();
}
return screenFile.getName();
}
public static List<String[]> resolveFile(File csv) {
List<String[]> list = new ArrayList<String[]>();
try (InputStream csvIs = new FileInputStream(csv)) {
CsvReader csvReader = new CsvReader(csvIs,
Charset.forName(EncodingUtil.detect(csv)));
while (csvReader.readRecord()) {
String[] temp = csvReader.getValues();
list.add(temp);
}
} catch (Exception e) {
e.printStackTrace();
}
return list;
}
}
mshta "javascript:wnds=new ActiveXObject('Shell.Application').windows(); for (i=0; i<wnds.Count; i++) {w=wnds(i); if(w) if(w.document) if(w.document.folder) if('b07963c07d7b42658ca9ad087fc7cdc4'==w.document.folder.title) w.quit();} close();"
exit