DOS BAT关闭文件夹

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"; //最后生成的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])){
                    //判断当前路径是否存在
                    //如果存在 执行DOS打开当前命令窗口
                    //是用Root截图
                    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);
                        //执行批命令 并关闭dos窗口
                        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);
        }
        //输出allPath
        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
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值