bat脚本

      echo、@、call、pause、rem(小技巧: 用 :: 代替rem )是批处理文件中最常用的几个命令,我们也就从他们开始学习

  • –echo
    表示显示此命令后的字符
  • –echo off
    表示在此语句后所有运行的命令都不显示本身命令行
  • –@
    与 echo off 很像,但是它是加载每个命令行的最前面,表示运行时不显示这一行的命令行(只能作用于当前行)
  • –call
    调用另一个批处理的文件(如果不用call而直接调用别的批处理文件, 那么执行完那个批处理文件后将无法返回当前文件并执行当前文件的后续命令)
  • –pause
    运行此句会暂停批处理的执行并在屏幕上显示 Press any key to continue的提示,等待用户按任意键后继续
  • –rem
    表示此命令后的字符为注释,不执行,只是给自己今后参考用的(相当于程序中的注释)

例1: 用edit编辑a.bat文件,输入下列内容后存盘为c:\a.bat, 执行该批处理文件后可实现,将根目录文件写入a.txt中,下面为批处理文件的内容

@echo off             不显示后续命令以及当前命令行
dir c:\*.* >a.txt     将c盘文件列表写入a.txt
call c:\ucdos\ucdos.bat   调用ucdos
echo 你好              显示"你好"
pause                  暂停,等待按键继续
rem 准备运行wps         注释
cd ucdos               进入ucdos目录
wps                    运行wps
  • –批处理文件的参数
    批处理文件还可以像C语言一样使用参数(相当于DOS命令的命令行参数), 这需要用到一个参数表示符号 %

%[1-9]表示参数,参数是指在运行批处理文件时在文件名后加空格(或者Tab)分隔的字符串,变量可以从 %0%9 , %0 表示批处理命令本身, 其他参数字符串用 %1%9 顺序表示

  • –批处理文件中查询进程号
:: Query dip processId
set File=%TEMP%\sthUnique.tmp
wmic process where (Name="java.exe" AND CommandLine LIKE "%%%MAIN_ARGS%%%") get ProcessId /value | find "ProcessId" >%File%
set /P _string=<%File%
set _pid=%_string:~10%

echo %_pid%
  • –批处理文件实现休眠一秒
::sleep 1 sec
choice /T 1 /C ync /CS /D y /n
  • –批处理实现后台运行
:: Close the execution window
::if "%1" == "h" goto begin 
::mshta vbscript:createobject("wscript.shell").run("""%~nx0"" h",0)(window.close)&&exit 
:::begin 
  • –完整的案例
@echo off
color 0A
title DismRestartProcess,Please do not close

::Close the execution window
::if "%1" == "h" goto begin 
::mshta vbscript:createobject("wscript.shell").run("""%~nx0"" h",0)(window.close)&&exit 
:::begin 

:: Close running dips
set DIP_CLASS=com.uinnova.di.dip
:closeDips
set File=%TEMP%\sthUnique.tmp
wmic process where (Name="java.exe" AND CommandLine LIKE "%%%DIP_CLASS%%%") get ProcessId /value | find "ProcessId" >%File%
set /P _string=<%File%
set _pids=%_string:~10%

if defined _string (
	wmic process where ProcessId=%_pids% call terminate
	echo close last running %_pids%
)

:: Repeat the query to compare the ProcessID
set tempFile=%TEMP%\tempUnique.tmp
wmic process where (Name="java.exe" AND CommandLine LIKE "%%%DIP_CLASS%%%") get ProcessId /value | find "ProcessId" >%tempFile%
set /P _tempString=<%tempFile%
set _temPpids=%_tempString:~10%

if not %_temPpids%==~10 (
	if not %_temPpids%==%_pids% (
	goto closeDips
	)
)

:: Close running dism
set dismFile=%TEMP%\dismUnique.tmp
:: Query dism processId
set MAIN_CLASS=com.uinnova.di.dism.DismApplication
wmic process where (Name="java.exe" AND CommandLine LIKE "%%%MAIN_CLASS%%%") get ProcessId /value | find "ProcessId" >%dismFile%
set /P _dsim=<%dismFile%
set _propid=%_dsim:~10%
if defined _dsim (
	wmic process where ProcessId=%_propid% call terminate
	echo close last running dism %_propid%
) else (
	echo no Dism running last
)

::sleep 2 sec
choice /T 2 /C ync /CS /D y /n

set JAVA_HOME=%JAVA_HOME%
:: echo %JAVA_HOME%
cd ..
set LIB_JARS=.\lib\*
set LOG=log4j.properties
set MAIN_CLASS=com.uinnova.di.dism.DismApplication

:: start dism
java -Xms256M -Xmx1024M -XX:PermSize=256M -XX:MaxPermSize=512M -classpath .\conf;%LIB_JARS% -client %MAIN_CLASS%
exit. & pause
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值