如何通过批处理脚本查找.sql后缀文件并执行内容检索、补充操作?
调整后的完整批处理脚本
@ECHO OFF setlocal enabledelayedexpansion set "writePath=E:\Base_Package_oracle\oracle_rep" type nul>directoryList.txt for /F "tokens=*" %%f in ('dir /B /A:D /S "E:\Base_Package_oracle\*.*"') do ( if exist "%%~ff\*.sql" ( rem 处理当前目录下所有.sql文件 for %%s in ("%%~ff\*.sql") do ( set "has_commit=0" set "has_exit=0" set "has_slash=0" rem 校验三个字符串是否存在 findstr /C:"commit" "%%s" >nul 2>&1 if not errorlevel 1 set "has_commit=1" findstr /C:"exit" "%%s" >nul 2>&1 if not errorlevel 1 set "has_exit=1" findstr /C:"/" "%%s" >nul 2>&1 if not errorlevel 1 set "has_slash=1" rem 追加缺失内容,先插入换行避免和原有内容粘连 echo.>>"%%s" if !has_commit! equ 0 echo commit>>"%%s" if !has_exit! equ 0 echo exit>>"%%s" if !has_slash! equ 0 echo />>"%%s" ) rem 保留原有目录记录逻辑 echo "%%~ff">> directoryList.txt ) else ( echo file not found ) ) for /F "tokens=*" %%f in ('type directoryList.txt') do ( echo xcopy "%%~f" "%writePath%" /e/v/c/y/h/r/d ) PAUSE endlocal
关键修改说明
- 新增延迟变量扩展:避免for循环内的变量取值异常,脚本运行结束后自动还原系统环境变量配置
- 新增.sql文件遍历逻辑:识别到包含.sql文件的目录后,循环处理该目录下所有.sql后缀的文件
- 新增字符串校验逻辑:用
findstr命令分别校验commit、exit、/三个字符串是否存在于文件中,校验过程不输出多余信息 - 新增缺失内容追加逻辑:校验到缺失对应字符串时,自动追加到文件末尾,追加前会先插入换行避免和原有最后一行内容粘连
- 完全保留原有目录输出、文件复制逻辑,不影响你原本的功能需求
注意:如果需要忽略大小写校验字符串,可以给
findstr添加/I参数,例如findstr /I /C:"commit" "%%s"
内容的提问来源于stack exchange,提问作者Ankit
相关产品推荐
相关产品推荐

