window bat 学习
遍历当前文件
@echo off echo %cd% set work_path=%cd% echo %work_path% cd %work_path% rem for /f "delims=" %a in ('dir %cd%') do set FilePath=%~a rem echo %FilePath% for %%s in (.,*) do ( echo %%s ) pause
https://blog.csdn.net/zhanglh046/article/details/50529650
@echo off chcp 936 echo %cd% echo set svnvercmd=%~dp0\svnversion rem 该文件当前目录 set svncmd=%~dp0\svn %svncmd% update ..\ %svnvercmd% ..\ for /f " tokens=1 delims= " %%a in ('%svnvercmd% ..\') do set VER=%%a echo Current Version £º%VER% ---------------- echo #define APP_SVN_VER "%VER%" >svn_version.h rem pause
@echo off setlocal enabledelayedexpansion echo %cd% set work_path=%cd% rem echo %work_path% cd %work_path%for /d %%i in (L*) do ( set win32dir=%work_path%\%%i\trunk\release\win32\include set dstDir=%work_path%\winLib\include call :sub !win32dir! !dstDir! set win32dir=%work_path%\%%i\trunk\release\win32\lib set dstDir=%work_path%\winLib\lib call :sub !win32dir! !dstDir! ) pause :sub if not exist %2 ( echo 创建%2目录 md %2 ) for /r %1 %%b in (*.*) do ( rem echo %%b rem echo %2 rem 获取文件名称 SET "FILE_PATH_NO_EXT=%%~nxb" rem echo !FILE_PATH_NO_EXT! set dst=%2\!FILE_PATH_NO_EXT! set src=%%b rem echo !dst! rem echo !src! if not exist !dst! ( mklink !dst! !src! ) ) goto :eof
bat for循环嵌套
https://zhidao.baidu.com/question/376257987.html
如果您在批处理脚本中将 %%~nxF 的值赋给一个变量,并在之后尝试打印该变量的值,但没有看到预期的输出,可能是由于延迟扩展导致的。在批处理中,延迟扩展是一种特殊的变量处理方式,可以通过启用 enabledelayedexpansion 实现。
在使用延迟扩展时,需要使用 ! 来包围变量名称,而不是 %
遍历Output文件夹下最新的lib文件
setlocal enabledelayedexpansion set "destinationDir=..\..\SDK_OS_SLG0812_B1\SDK_OS_SLG0812_B1\SDK_Files\lib_files" set "targetDir=Output" set "latestFile=" set "latestTimestamp=2021/05/01 17:48" for %%F in ("%targetDir%\*.lib") do ( set "currentFile=%%~F" for %%A in ("!currentFile!") do set "fileTimestamp=%%~tA" rem echo File name: %currentFile% rem echo File Timestamp1: %fileTimestamp% if "!fileTimestamp!" gtr "!latestTimestamp!" ( set "latestTimestamp=!fileTimestamp!" set "latestFile=%%~dpnxF" set "CurrFileName=%%~nF" rem set "CurrFolderName=%%~dpF" rem echo File 1 is newer than File 2. ) )
endlocal
setlocal enabledelayedexpansion 是Windows批处理脚本中的一个命令,用于启用延迟变量扩展。
在批处理脚本中,正常情况下,变量是在脚本的每行逐一解析的。这意味着,如果在一个代码块中(例如for循环或if语句)更改了一个变量的值,那么在同一个代码块内后续的行将仍然使用该变量的原始值。这可能导致意外行为或错误的结果。
启用了延迟变量扩展后,您可以使用!符号而不是%符号来访问变量,并且它们将在运行时扩展,而不是在解析时。这使得在代码块内更改变量的值并正确使用新值变得更容易。
加上setlocal enabledelayedexpansion 就是在运行过程中实时获取变量而不是旧变量
对文件加上md5并将md5附加到文件名上
@echo off
chcp 65001
setlocal enabledelayedexpansion
:: 删除以 "外机" 开头的文件
for %%A in ("外机*") do if exist "%%A" del "%%A"
:: 读取版本号
set "filePath=.\APP_Files\User_Conf\app_Conf.h"
for /f "tokens=2,3" %%a in ('type "%filePath%" ^| findstr /i "#define APP_VERSION_"') do (
if "%%a"=="APP_VERSION_MAIN" (
set "APP_VERSION_MAIN=%%b"
) else if "%%a"=="APP_VERSION_SUB1" (
set "APP_VERSION_SUB1=%%b"
) else if "%%a"=="APP_VERSION_SUB2" (
set "APP_VERSION_SUB2=%%b"
) else if "%%a"=="APP_VERSION_RC" (
set "APP_VERSION_RC=%%b"
)
)
:: 生成文件
set "filename=SLG1010_R410A_HV_10_16KW_OD"
set "file=.\Output\%filename%.bin"
set "outputAppFileName=外机%filename%_V%APP_VERSION_MAIN%.%APP_VERSION_SUB1%.%APP_VERSION_SUB2%.%APP_VERSION_RC%.bin"
copy /b /Y "%file%" "%outputAppFileName%"
set "file=.\Output\%filename%.hex"
set "outputHexAppFileName=外机%filename%_V%APP_VERSION_MAIN%.%APP_VERSION_SUB1%.%APP_VERSION_SUB2%.%APP_VERSION_RC%.hex"
copy /b /Y "%file%" "%outputHexAppFileName%"
:: 计算 MD5 并重命名文件
set "files=%outputAppFileName% %outputHexAppFileName%"
for %%F in (%files%) do (
set "filetemp=%%~F" :: 去掉文件路径中的双引号
:: 获取 MD5(排除无关行)
set "md5="
for /f "skip=1 tokens=1 delims=" %%A in ('certutil -hashfile "!filetemp!" MD5 ^| findstr /r "^[0-9A-Fa-f]"') do (
if not defined md5 set "md5=%%A"
)
:: 去除 MD5 值中的空格
set "md5=!md5: =!"
:: 确保 MD5 值正确
if not defined md5 (
echo 错误: 未能计算 "!filetemp!" 的 MD5 值!
exit /b
)
echo !filetemp! 的 MD5: !md5!
:: 解析文件名和扩展名
for %%X in ("!filetemp!") do set "filenametemp=%%~nX" & set "ext=%%~xX"
:: 生成新文件名
set "newfile=!filenametemp!_!md5!!ext!"
echo !filetemp! -> !newfile!
:: 删除已有的同名文件,避免 `rename` 失败
if exist "!newfile!" del "!newfile!"
:: 进行重命名
rename "!filetemp!" "!newfile!"
)
endlocal

浙公网安备 33010602011771号