批处理脚本功能案列总结

网友投稿 290 2022-09-04

批处理脚本功能案列总结

一:实现自动安装字体

rem 自动安装字体文件到 windows 系统上注: 字体文件与 bat 脚本文件在同一个目录下。如上图@echo offsetlocal EnableDelayedExpansioncd %~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 call=new Callable() { public Integer call() throws Exception { Integer status=process.waitFor(); return status; } }; ScheduledExecutorService singleThreadScheduledExecutor = Executors.newSingleThreadScheduledExecutor(); Future 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(); } } }}

版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:SqlServer 分页存储过程
下一篇:东京奥运品牌营销榜 · 终榜,奥运营销6大观察!
相关文章

 发表评论

暂时没有评论,来抢沙发吧~