1. 程式人生 > 實用技巧 >【2020-11-27】windows bat整理

【2020-11-27】windows bat整理

bat 指令碼整理

參考:https://www.jianshu.com/p/dc3ffc784519Windows 批處理指令碼指南

延遲擴充套件講解:https://www.cnblogs.com/happy-rabbit/p/6283787.html

1. 常見命令

rem   註釋1
::    註釋2,注意在for迴圈中使用會導致錯誤,需要使用rem來註釋

@          關閉本命令的回顯
@echo on   開啟命令回顯
@echo off  關閉命令回顯

SET VarName=VarValue       設定變數,注意不要有空格,儘量用小寫(因為環境變數一般是大寫;注意不要覆蓋了環境變數)
SET /A tmp=2+2 支援算數 SET /P tmp=testing tmp由輸入的data賦值 SET 輸出當前的所有變數 動態變數 %CD% - 擴充套件到當前目錄字串。 %DATE% - 用跟 DATE 命令同樣的格式擴充套件到當前日期。 %TIME% - 用跟 TIME 命令同樣的格式擴充套件到當前時間。 %RANDOM% - 擴充套件到 0 和 32767 之間的任意十進位制數字。 %ERRORLEVEL% - 擴充套件到當前 ERRORLEVEL 數值。 %CMDEXTVERSION% - 擴充套件到當前命令處理器擴充套件版本號。 %CMDCMDLINE% - 擴充套件到呼叫命令處理器的原始命令列。 %HIGHESTNUMANODENUMBER% - 擴充套件到此計算機上的最高 NUMA 節點號。
setlocal 在endlocal之前,set的變數都是區域性變數 endlocal setlocal enabledelayedexpansion 開啟延遲擴充套件 setlocal disabledelayedexpansion 關閉延遲擴充套件 endlocal setlocal enableextension 開啟命令擴充套件==預設開啟的,沒看明白有什麼用。。 setlocal disableextension 關閉命令擴充套件 endlocal %1 %2 指令碼、函式傳入的第一個引數、第二個引數...(最多支援到%9
,9個引數)
%* 指令碼、函式傳入的所有引數

:: parameter extension https://ss64.com/nt/syntax-args.html
%~f1 - expands %1 to a Fully qualified path name - C:\utils\MyFile.txt
%~d1 - expands %1 to a Drive letter only - C:
%~p1 - expands %1 to a Path only - \utils\
%~n1 - expands %1 to a file Name, or if only a path is present (with no trailing backslash) - the last folder in that path
%~x1 - expands %1 to a file eXtension only - .txt
%~s1 - changes the meaning of f, n and x to reference the Short name (see note below)

%~1 - expand %1 removing any surrounding quotes (")
%~a1 - display the file attributes of %1
%~t1 - display the date/time of %1
%~z1 - display the file size of %1
%~$PATH:1 - search the PATH environment variable and expand %1 to the fully qualified name of the first match found.

The modifiers above can be combined:

%~dp1 - expands %1 to a drive letter and path only

%~nx2 - expands %2 to a file name and extension only


cmd1 && cmd2 利用 && || 短路執行,控制程式的執行命令和退出
cmd1 ||exit/b 1 出錯退出
cmd1 || goto :eof 出錯退出
>      覆蓋式輸出 
< 輸入
>> 追加輸出
> NUL as >null in linux

TYPE CON fileName cmd輸入的文字轉輸出到檔案(Ctrl+C停止輸入)
TYPE NUL > w.txt 建立空檔案
echo. > w.txt 建立空檔案

2. 示例

rem usage: xxx.bat buildnumber versionMajor.versionMinor.versionRe

rem use input parameters to set buildnumber and version

if NOT "%1"=="" SET build=%1
if NOT "%2"=="" (
    for /f "tokens=1,2,3 delims=." %%i in ("%2") do (
       SET ver_major=%%i
       SET ver_minor=%%j
       SET ver_reversion=%%k
)

rem change rc_file's src_str to dst_str write into rc_file_dst_tmp, and then copy rc_file_dst_tmp  to rc_file_dst
SET
rc_file=123.txt SET rc_file_dst_tmp=456.txt SET rc_file_dst=789.txt SET Year=%date:~0,4% set src_str=YEAR set dst_str=%Year:~0,-1% rem function in bat and calling, rem ChangeDate srcFilePath dstFilePathTmp dstFilePath srcStr dstStr call :ChangeDate %rc_file% %rc_file_dst_tmp% %rc_file_dst% %src_str% %dst_str% del %rc_file_dst_tmp% rem should use goto :eof jump over function defination goto :eof :ChangeDate ( echo %~1 %~2 %~3 %~4 %~5 rem echo > %~2 rem enabledelayedexpansion ! will be ignored for /f "delims=" %%i in ('type "%~1"') do ( set str=%%i setlocal enabledelayedexpansion set str=!str:%~4=%~5! echo !str! >> %~2 endlocal ) copy %~2 %~3 goto:eof )