window bat脚本编写

简介: 一、window 环境脚本语法if 语句Usage 1:bat脚本中字符串不用加“”号,如果添加后则许两重双引号才能相等set computername=xyzset dropLoc=machine-abcif "%computerna...

一、window 环境脚本语法

  1. if 语句

Usage 1:

bat脚本中字符串不用加“”号,如果添加后则许两重双引号才能相等

set computername=xyz
set dropLoc=machine-abc
if "%computername%" == "xyz" (
set dropLoc=machine-xyz
)
echo %dropLoc%
Usage 2:

goto debug表示调用debug函数

rem 这是注释
set DEBUG_OPTS=
if ""%1"" == ""debug"" (
set DEBUG_OPTS= -Xloggc:../logs/gc.log -verbose:gc -XX:+PrintGCDetails -XX:+HeapDumpOnOutOfMemoryError -XX:HeapDumpPath=../logs
goto debug
)
springboot启动脚本

echo off

set APP_NAME=springboot-vue.jar
set CONFIG= -Dlogging.path=../logs -Dlogging.config=../config/log4j2.xml -Dspring.config.location=../config/application.yml

set DEBUG_OPTS=
if ""%1"" == ""debug"" (
set DEBUG_OPTS= -Xloggc:../logs/gc.log -verbose:gc -XX:+PrintGCDetails -XX:+HeapDumpOnOutOfMemoryError -XX:HeapDumpPath=../logs
goto debug
)

set JMX_OPTS=
if ""%1"" == ""jmx"" (
set JMX_OPTS= -Dcom.sun.management.jmxremote -Dcom.sun.management.jmxremote.port=9888 -Dcom.sun.management.jmxremote.ssl=FALSE -Dcom.sun.management.jmxremote.authenticate=FALSE
goto jmx
)

echo "Starting the %APP_NAME%"
java -Xms512m -Xmx512m -server %DEBUG_OPTS% %JMX_OPTS% %CONFIG% -jar ../lib/%APP_NAME%
goto end

:debug
echo "debug"
java -Xms512m -Xmx512m -server %DEBUG_OPTS% %CONFIG% -jar ../lib/%APP_NAME%
goto end

:jmx
java -Xms512m -Xmx512m -server %JMX_OPTS% %CONFIG% -jar ../lib/%APP_NAME%
goto end

:end
pause
bat处理替换xml文件中的字符

@echo off

set filename=mylove.xml
setlocal DisableDelayedExpansion
SET MyVar=../logs
set oldStr=logs
(for /F "delims=" %%G in (%filename%) do (
set "str=%%G"
setlocal EnableDelayedExpansion
set "strTrim={x}!str!{x}"
if "!strTrim!"=="{x}{x}" (
CALL :BlankLine
) ELSE (
set "str=!str:%oldStr%=%MyVar%!"
IF "!str!"=="" (
CALL :BlankLine
) ELSE (
set "strTrim=!strTrim: ={x}!"
set "strTrim=!strTrim: ={x}!"
set "strTrim=!strTrim:{x}{x}={x}!"
set "strTrim=!strTrim:{x}{x}={x}!"
:: I had to repeat the set "strTrim=!strTrim:{x}{x}={x}!" several times to get rid of all combinations of blanks - I could also have edited the template, of course...
IF "!strTrim!"=="{x}" (CALL :BlankLine) ELSE (ECHO !str!)
)
)
endlocal
)) > filename.xml

move "%filename%.tmp" "%filename%"

goto :eof

:BlankLine
ECHO(
goto :eof
在xml文件的字符行下新添加一行

setlocal enabledelayedexpansion
set linecount=0
set match_line=0;
(
FOR /F "delims=" %%A IN (d:\settings.xml) DO (
setlocal DisableDelayedExpansion
ECHO %%A
endlocal

IF "%%A" EQU "  <localRepository>/path/to/local/repo</localRepository>" (
  set /A match_line=!linecount!+1
)
if !linecount! equ !match_line! (
TYPE d:\Line_to_add.txt
echo.
)
set /a linecount=!linecount!+1

)
) >d:\temp.xml
move /y d:\temp.xml d:\settings.xml
对于xml文件的操作,读取每一行内容是不能使用延迟变量,否则文件中的!特殊字符将会被脚本移除,要保持原有空格必须在for中强制使用"delims="

batch中条用powershell下载文件

PowerShell.exe curl https://search.maven.org/remotecontent?filepath=kg/apc/jmeter-plugins-manager/0.20/jmeter-plugins-manager-0.20.jar -O jmeter-plugins-manager-0.20.jar
-O是指定下载路径

获取某个目录下文件(包含子目录)

@echo off
::
set num=0
setlocal enabledelayedexpansion
set conf_dir=../config
::匹配所有文件,匹配制定后缀文件可写为.properties,.xml
for /R "%conf_dir%" %%s in (
) do (
::set str=!str!,%conf_dir%/%%~nxs
set /a num+=1
IF !num! equ 1 (
set str= !str!%conf_dir%/%%~nxs
) ELSE (
set str= !str!,%conf_dir%/%%~nxs
)
)
echo %str%
pause
~nx表示不显示文件的路径名

检查某个命令是否存在,例如检查windows是否安装了wget

WHERE wget >nul 2>nul
IF %ERRORLEVEL% EQU 0 (
ECHO scp found
) else (
GOTO :EOF
)
检查服务

SC QUERY | FIND "nexus"
IF %ERRORLEVEL% EQU 0 (

echo stop

)else (
echo start
)
设置环境变量

set wget_home=D:\ProgramFiles\wget
set path_temp=%wget_home%;%Path%
set myPath=%wget_home%
For /F "Delims=" %%I In ('echo %Path% ^| find /C /I "%myPath%"') Do set pathExists=%%I 2>Nul
If %pathExists%==0 (
wmic ENVIRONMENT where "name='Path' and username='<system>'" set VariableValue="%path_temp%"
)else (
echo INFO: %myPath% exists in PATH
)
win7+文本替换,使用powershell

@echo off
set ffile='myfile.txt'
set fold='FOO'
set fnew='BAR'
powershell -Command "(gc %ffile%) -replace %fold%, %fnew% | Out-File %ffile% -encoding utf8"
替换字符串中的字符

将\替换成/

set final_config_location=!config_location:=/!

相关文章
|
6月前
|
Shell 开发工具
编写与执行一个shell script
【1月更文挑战第3天】编写与执行一个shell script。
120 1
|
Web App开发 JavaScript 前端开发
如何运行javascrip的脚本
如何运行javascrip的脚本
45 0
Window 命令行查看 EXE或者DLL的依赖库
Window 命令行查看 EXE或者DLL的依赖库
810 0
|
Python
启动appium服务的2种方法(python脚本&cmd窗口)
启动appium服务的2种方法(python脚本&cmd窗口)
957 0
启动appium服务的2种方法(python脚本&cmd窗口)
|
Shell 开发工具 git
easyswoole 更新代码shell
easyswoole 更新代码shell
76 0
一种切实可行的后台执行脚本方法——利用VB脚本隐藏CMD命令窗口
一种切实可行的后台执行脚本方法——利用VB脚本隐藏CMD命令窗口
399 0
phpstorm设置断点调试
phpstorm设置断点调试
181 0
phpstorm设置断点调试
|
资源调度 JavaScript Ubuntu
Cypress系列(1)- Window下安装 Cypress 并打开
Cypress系列(1)- Window下安装 Cypress 并打开
347 0
Cypress系列(1)- Window下安装 Cypress 并打开