FORFILES をバッチで使用して、1 週間以上経過した特定のサフィックス ファイルを削除します。

FORFILES をバッチで使用して、1 週間以上経過した特定のサフィックス ファイルを削除します。

localhost_access_logFORFILESをバッチで使用して、1週間以上前の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

%%tforfilesが完全なパスである場合、それは前のループからの結果であり、100%修飾された要素ですが、たとえ追加されたとしても、によって使用されるマスクではありません。*マスクとして扱われません。

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

追加リソース:

関連情報