• 批处理学习以及批处理脚本案例


    批处理脚本语法总结:

    批处理脚本语法学习:
    @echo "Hello world"   rem 类似于        System.out.println();  输出语句
    md    D:mybat       //将在D盘下创建一个名为 mybat的文件夹
    rd      D:mybat        //将删除D盘中的mybat文件夹删除
    del     D:mybatsheepmu.txt          //删除文件sheepmu.txt
    del     D:mybatsheepmu.txt    /q            //安静模式删除,即删除时不弹出是否删除的提示框
    xcopy   D:mybatsheepmu.txt    D:mybat1          //若D中无mybat1这个文件夹则会创建文件夹再复制
     e.向文件中写入内容:  echo   sheepmu >> D:mybatsheepmu.txt         //如果这个文件不存在就创建文件并输入。如果是文件 夹不存在就不会成功! >>与>的区别是>是覆盖掉原来  的;>>是直接输入到原来的尾部
    f.显示文件中内容:                 type    D:mybatsheepmu.txt
    g.更改文件名后缀:                  ren    D:mybat*.txt   *.html  
    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 石
     *
     */
    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 石
    	 *
    	 */
    	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();
    			}
    		}
    	}
    
    }
    

      

    
    
  • 相关阅读:
    Python Challenge 第十二关
    Python Challenge 第十一关
    Python Challenge 第十关
    Python Challenge 第九关
    Python Challenge 第八关
    Python Challenge 第七关
    zepto
    zepto
    zepto
    zepto
  • 原文地址:https://www.cnblogs.com/sunliyuan/p/13516159.html
Copyright © 2020-2023  润新知