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:=/!

相关文章
|
23天前
|
Shell 开发工具
编写与执行一个shell script
【1月更文挑战第3天】编写与执行一个shell script。
78 1
|
7月前
|
JavaScript Shell 开发工具
使用 npm 的配置参数 script-shell 来避免 window 执行脚本失败
使用 npm 的配置参数 script-shell 来避免 window 执行脚本失败
47 1
|
9月前
|
Web App开发 JavaScript 前端开发
如何运行javascrip的脚本
如何运行javascrip的脚本
31 0
|
Shell 开发工具 git
easyswoole 更新代码shell
easyswoole 更新代码shell
59 0
一种切实可行的后台执行脚本方法——利用VB脚本隐藏CMD命令窗口
一种切实可行的后台执行脚本方法——利用VB脚本隐藏CMD命令窗口
337 0
|
JavaScript 前端开发
【sublime】sublime Text 3 javaScript代码自动提示插件&安装步骤 &启动Debug模式
最近使用sublime开发node.js,但是sublime的js代码在书写的时候并没有提示功能。 因此搜到资料,用于安装代码自动提示插件。   1.打开sublime,然后快捷键Ctrl+Shift+P,打开pacakges列表界面,搜索packages:install packages 【注意】...
2050 0
|
资源调度 JavaScript Ubuntu
Cypress系列(1)- Window下安装 Cypress 并打开
Cypress系列(1)- Window下安装 Cypress 并打开
300 0
Cypress系列(1)- Window下安装 Cypress 并打开
phpstorm设置断点调试
phpstorm设置断点调试
144 0
phpstorm设置断点调试
Cypress系列(34)- window() 命令详解
Cypress系列(34)- window() 命令详解
273 0
Cypress系列(34)- window() 命令详解
|
Web App开发 JavaScript
VBS一键配置VOIP脚本(其中包括VBS操作JS网页中的按钮事件--直接执行确认按钮中的脚本代码)
Dim ws,fso,IESet IE = WScript.createobject("InternetExplorer.Application")Set ws = WScript.CreateObject ("WSCript.
737 0

热门文章

最新文章