查找目录下指定类型的所有文件(maven 打包提取脚本)

1 首先想到的是递归遍历目录 筛选出符合条件的文件

  dir命令递归遍历目录 /b控制显示格式 /s递归  /ad 只显示目录

dir /b/s .*

判断文件类型

操作数得用``

rem 取出文件扩展名
%%~XI
rem 判断相等 
if `%%~XI` equ `.jar`( echo %%I )

遍历 if括号里写需要的操作

@echo off
for /f "delims=" %%a in ('dir /b/s ".*" ') do (
    if `%%~xa` equ `.jar` (
        echo %%a  
    )
)

2 dir 命令支持通配符

for /f "delims=" %%a in ('dir /b/s "*.jar" ') do (
   echo %%a
)

另附contains的两种实现

echo %%a|find "substring" >nul
if errorlevel 1 (echo notfound) else (echo found)
@echo off
setlocal EnableDelayedExpansion
set "substring=#"
for /f "delims=," %%a in (Text.txt) do (
    set "string=%%a"
    if "!string:%substring%=!"=="!string!" (
        rem string with substring removed equals the original string,
        rem so it does not contain substring; therefore, output it:
        echo(!string!
    )
)
endlocal

maven打包提取脚本

rem 停止服务
taskkill /f /im java.exe
set app=fi
set su=gl
set bo=AccountBalance
set projDir=bo-accountbalance
set srcRoot=code


set srcroot=E:projects\%app%\%su%\%bo%\%projDir%java%srcRoot%

call mvn clean   -f %srcroot%
call mvn package -f %srcroot% -B -DskipTests
echo %ERRORLEVEL%
IF %ERRORLEVEL% NEQ 0 (
    pause
    goto end
)
rem 拷贝jar包
for /f "delims=" %%a in ('dir /b/s "%srcroot%*.jar"') do ( 
    xcopy %%a %EnvPath%jstack\%app%\%su%libs /R /Y /e/s/k/d/f
)

rem 启动服务
cd /D %EnvPath%
call  %EnvPath%startup-jstack.cmd
:end
pause

参考:

https://stackoverflow.com/questions/23209474/xcopy-wildcard-source-folder-name-to-destination

https://stackoverflow.com/questions/34077831/how-to-see-if-a-string-contains-a-substring-using-batch/34077870

原文地址:https://www.cnblogs.com/wolbo/p/11787693.html