![일주일보다 오래된 특정 접미사 파일을 일괄 삭제하는 FORFILES](https://rvso.com/image/1646052/%EC%9D%BC%EC%A3%BC%EC%9D%BC%EB%B3%B4%EB%8B%A4%20%EC%98%A4%EB%9E%98%EB%90%9C%20%ED%8A%B9%EC%A0%95%20%EC%A0%91%EB%AF%B8%EC%82%AC%20%ED%8C%8C%EC%9D%BC%EC%9D%84%20%EC%9D%BC%EA%B4%84%20%EC%82%AD%EC%A0%9C%ED%95%98%EB%8A%94%20FORFILES.png)
localhost_access_log
FORFILES를 일괄적으로 사용하여 일주일이 지난 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"
삼.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>
, 등.
- 조건부 실행 || && ...