배치 스크립트에서 "에코 일치 확장자"가 있는 파일 확장자를 제거하시겠습니까?

배치 스크립트에서 "에코 일치 확장자"가 있는 파일 확장자를 제거하시겠습니까?

이 배치 스크립트가 있습니다.

set driveletter=F

call :delext "*.foo"
call :delext "*.bar"
call :delext "*.pdf"


:: funcion delext
@echo off
pause
goto:eof
:delext
  set delext=%1
  del /f/q/s %driveletter%:\"%delext%"
goto:eof

확장명과 일치하는 것이 있으면 "에코"가 필요합니다.

예를 들어, 라는 파일이 있고 test.pdf확장자와 일치하므로 출력에 *.pdf에코가 필요합니다 match pdf(일치하는 파일이 없으면 아무 것도 표시하지 않습니다.)

어떻게 해야 합니까?

답변1

나는 함수형 배치 프로그래밍의 열렬한 팬입니다.

내가 거의 잠을 충분히 자지 않았기 때문에 여기서 내 솔루션은 엉성할 수도 있지만 그것이 작동하고 귀하가 요구하는 작업을 어느 정도 수행한다는 것을 확신할 수 있습니다.

이는 이 문제를 해결하는 가장 비효율적인 방법일 수 있지만 디렉터리별로 모든 확장을 수행하도록 배치를 작성한 경우 해당 확장에 대해 설명하는 경우 추적이 좀 더 복잡해지지만 실행 속도는 빨라집니다.

또한 변수를 사용하지 않으면 함수가 아주 작은 코드로 줄어들어 무슨 일이 일어났는지 따라갈 수 있습니다.

@echo off

:: Set the starting path that we will be searching.
Set pathToSearch=C:\users\yo_mamma
cd /d "%pathToSearch%"
if not "%ERRORLEVEL%"=="0" echo Path %PathToSearch% not found&&exit /b 1


Set ExtensionsToCheck="*.foo" "*.txt" "*.bar" "*.pdf"
call :EnumerateExtensions %ExtensionsToCheck%
goto :EOF

::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
:: Function "EnumerateExtensions"
:EnumerateExtensions
if "%~1"=="" goto :EOF
Set fileMatchFound=FALSE
Set searchFileMask=%1
for /r %%p in ('.') do call :EnumerateDirectories "%%p" %searchFileMask%
shift
goto :EnumerateExtensions

::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
:: Function "EnumerateDirectories"
:EnumerateDirectories
Set directoryPath=%~DP1
Set fileMask=%~2
for %%f in (%fileMask%) do call :MatchFound "%directoryPath%" "%fileMask%" "%%f"
goto :EOF

::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
:: Function "MatchFound"
:MatchFound
if not "TRUE"=="%fileMatchFound%" echo ****** Mask match found:  %~2&&Set fileMatchFound=TRUE
:: echo del %~1%~3 <-- delete the found file here..
goto :EOF

관련 정보