Java调用bat文件

自动获取管理员权限命令

@echo off
mode con lines=30 cols=60
%1 mshta vbscript:CreateObject("Shell.Application").ShellExecute("cmd.exe","/c %~s0 ::","","runas",1)(window.close)&&exit
cd /d "%~dp0"
pause

批量创建环境变量

@echo off 
wmic ENVIRONMENT where "name='JRE64_HOME'" delete
wmic ENVIRONMENT where "name='TC_JRE_HOME'" delete
wmic ENVIRONMENT create name="TC_JRE_HOME",username="<system>",VariableValue="C:\Java\jre7"
wmic ENVIRONMENT create name="JRE64_HOME",username="<system>",VariableValue="C:\Java\jre7"
pause

hosts文件关键词删除行

@echo off
set Row=
set Str=
set "Xstr=BEARTCTEXT"
set "Files=C:\Windows\System32\drivers\etc\hosts"
set "OutputFiles=C:\Windows\System32\drivers\etc\$"
if exist "%Files%" (
setlocal enabledelayedexpansion
for /f "delims=" %%a in (%Files%) do (
    set/a Row+=1 & set "Str=%%a"
    ((echo !Str! | findstr /i "!Xstr!">nul) && (
        echo Found target text.(发现目标文字!)
    )) || (
        echo !Str!>>!OutputFiles!
    )
))
move !OutputFiles! %Files%>nul 2>nul
echo Completed (执行完毕)!!
pause>nul

批量编辑hosts文件

@echo off 
set stHosts=192.168.1.20 plm20
echo.>>%SystemRoot%\system32\drivers\etc\hosts
FOR /F "eol=# tokens=1 delims=" %%i in (%SystemRoot%\system32\drivers\etc\hosts) do if "%stHosts%"=="%%i" exit  
echo %stHosts%>> %SystemRoot%\system32\drivers\etc\hosts
pause

生成快捷方式及其图标

echo off
echo 生成快捷方式...
set tcpath=D:\Siemens\Teamcenter\OTW11_TEST\rac\portal.bat
for /f "tokens=2,*" %%i in ('reg query "HKCU\Software\Microsoft\Windows\CurrentVersion\Explorer\Shell Folders" /v "Desktop"') do Set a=%%j
echo 你当前桌面路径是:%a%
set todesktopPath=%a%\Teamcenter.url
echo [InternetShortcut] >>%todesktopPath%
echo url="%tcpath%" >>%todesktopPath%
echo IconIndex=0 >>%todesktopPath%  
echo IconFile=D:\Siemens\Teamcenter\OTW11_TEST\rac\runner.exe>>%todesktopPath% 
echo 快捷方式已生成
pause>nul

Java生成并调用bat文件

public void setbat() {
		String cmd = "@echo off\r\n" + "ipconfig/all\r\n" + "pause";
		String url = ".\\查看ip.bat";
		FileWriter fw = null;
		try {
			// 生成bat文件
			fw = new FileWriter(url);
			fw.write(cmd);
			fw.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
		try {
			// 运行bat文件
			Process process = Runtime.getRuntime().exec(url);
			InputStream in = process.getInputStream();
			String line;
			BufferedReader br = new BufferedReader(new InputStreamReader(in));
			while ((line = br.readLine()) != null) {
				System.out.println(line);
			}
			in.close();
			process.waitFor();
			System.out.println("执行成功");
		} catch (Exception e) {
			System.out.println("执行失败");
		}
	}

猜你喜欢

转载自blog.csdn.net/qq_34239424/article/details/83893453