批处理脚本功能案列总结

一:实现自动安装字体

 

rem 自动安装字体文件到 windows 系统上
注: 字体文件与 bat 脚本文件在同一个目录下。如上图



@echo off
setlocal EnableDelayedExpansion
cd %~dp0  rem 相当于cd  到当前目录
for /f "delims=" %%i in ('dir /x /b *.?tf') do (
set "zt=%%~si"
mshta "javascript:new ActiveXObject('Shell.Application').NameSpace(20).CopyHere('!zt:\=\\!',0x0010);close()"
)
echo.
echo 字体安装完毕!
pause >nul

二:调用方式:

String batPath = "D:/文档/aa/aa.bat"; // 把你的bat脚本路径写在这里
Integer result=  ExecuteShellUtils.execute(batPath);
System.out.println(result);

三:java 执行 批处理或者shell 脚本的工具类:

package com.resafety.design.studio.controller;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.concurrent.Callable;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;

import com.resafety.util.LogUtils;

/**
 * 执行脚本工具类
 * @author shi
 *
 */
public class ExecuteShellUtils {

    /**
     * 执行脚本
     * @param shell
     * @return
     * @throws Exception
     */
    public static Integer execute(String shell) throws Exception{
        Runtime runtime = Runtime.getRuntime();
        Process process = runtime.exec(shell);
        ProcessClearStream error=new ProcessClearStream(process.getErrorStream(), "Error");
        ProcessClearStream output= new ProcessClearStream(process.getInputStream(),"OutPut");
        error.start();
        output.start();
        Callable<Integer> call=new Callable<Integer>() {
            public Integer  call() throws Exception {
                Integer status=process.waitFor();
                return status;
            }
        };
        ScheduledExecutorService singleThreadScheduledExecutor = Executors.newSingleThreadScheduledExecutor();
        Future<Integer> submit = singleThreadScheduledExecutor.submit(call);
        //5分钟脚本未执行完成认为线程阻塞了,强制结束线程
        Integer status = submit.get(5, TimeUnit.MINUTES);
        return status;
    }
    
    /**
     * 清空脚本缓存流类,防止线程阻塞
     * @author shi
     *
     */
    static class ProcessClearStream extends Thread{
        private InputStream inputStream;
        private String type;

        public ProcessClearStream(InputStream inputStream, String type) {
            this.inputStream = inputStream;
            this.type = type;
        }

        public void run() {
            try {
                InputStreamReader inputStreamReader = new InputStreamReader(
                        inputStream,"GBK");
                BufferedReader br = new BufferedReader(inputStreamReader);
                // 打印信息
                String line = null;
                while ((line = br.readLine()) != null) {
                    //LogUtils.debug(line);
                    System.out.println(line);
                }
                // 不打印信息
//                 while (br.readLine() != null);
            } catch (IOException ioe) {
                ioe.printStackTrace();
            }
        }
    }

}

 

posted @ 2020-08-17 09:57  石shi  阅读(389)  评论(0编辑  收藏  举报