bat 脚本整理
参考:https://www.jianshu.com/p/dc3ffc784519 Windows 批处理脚本指南
延迟扩展讲解: 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 )