window bat腳本編寫

1、window 環境腳本語法vue

1. if 語句java

Usage 1:spring

bat腳本中字符串不用加「」號,若是添加後則許兩重雙引號才能相等shell

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

Usage 2:windows

goto debug表示調用debug函數springboot

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啓動腳本app

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文件中的字符curl

@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文件的字符行下新添加一行maven

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:\=/!
相關文章
相關標籤/搜索