
我想批量使用 FORFILES 來僅刪除localhost_access_log
一周以上的 txt(以 開頭)檔案(不是子目錄)。但我越來越%%t was unexpected at this time.
for %%t in (.txt) do forfiles /p "C:\Program Files\Apache Software Foundation\Tomcat 8.0\logs\" /s /m *%%t /d -10 /c "cmd /c del @PATH"
答案1
for /f useback^tokens^=* %i in (`2^>nul forfiles /p "C:\Program Files\Apache Software Foundation\Tomcat 8.0\logs\" /s /m "*.txt" /d -10 /c "cmd /c echo=@Path"`)do @echo\%~ni|findstr/bil localhost_access_log >nul && echo\ del/q /f "%~fi"
1.不是有效的遮罩:/m *%%t
這%%t
是一個完整路徑,它是前一個循環的結果且 100% 合格的元素,但不是 所使用的掩碼forfiles
,即使添加到*
,它不會被視為掩碼。
2.您可以更換/m *%%t
到:
/m "*.txt"
3.替換For
為For /F
循環:
for /f useback^tokens^=* %i in (`2^>nul forfiles /p "C:\Program Files\Apache Software Foundation\Tomcat 8.0\logs\" /s /m "*.txt" /d -10 /c "cmd /c echo=@Path"`)do ...
4.使用echo\FileName
|
使用重定向來檢查它是否與您要刪除的檔案的名稱相符Findstr
,以及運算符&&
(return 0
) 執行del
如果名稱符合:
@echo\%~ni|findstr/bil localhost_access_log >nul && echo\ del /q /f "%~i"
5.測試並確認是否滿意,如果滿意,則去掉echo
即可有效刪除該檔案。
@echo\%~ni|findstr/bil localhost_access_log >nul && del /q /f "%~i"
觀察:- 使用For
循環您可以擴展變數:
%~i - expands %i removing any surrounding quotes (")
%~fi - expands %i to a fully qualified path file/dir name only
%~ni - expands %i to a file/dir name only
%~xi - expands %i to a file/dir extension only
%%~nxi => expands %%~i to a file/dir name and extension
Use the FOR variable syntax replacement: %~pI - expands %I to a path only %~nI - expands %I to a file name only %~xI - expands %I to a file extension only
The modifiers can be combined to get compound results: %~pnI - expands %I to a path and file name only %~pnxI - expands %I to a path, file name and extension only
其他資源:
Del /?
Echo /?
For /?
For /F /?
Forfiles /?
- 重定向
|
,<
,>
,2>
, ETC。
- 條件執行 || && ...