在批次腳本中刪除帶有「回顯匹配擴展名」的檔案副檔名?

在批次腳本中刪除帶有「回顯匹配擴展名」的檔案副檔名?

我有這個批次腳本:

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

相關內容