일주일보다 오래된 특정 접미사 파일을 일괄 삭제하는 FORFILES

일주일보다 오래된 특정 접미사 파일을 일괄 삭제하는 FORFILES

localhost_access_logFORFILES를 일괄적으로 사용하여 일주일이 지난 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

추가 리소스:

관련 정보