FORFILES im Batch, um bestimmte Suffixdateien zu löschen, die älter als eine Woche sind

FORFILES im Batch, um bestimmte Suffixdateien zu löschen, die älter als eine Woche sind

Ich möchte FORFILES im Batch verwenden, um NUR txt- localhost_access_logDateien (beginnend mit ) (keine Unterverzeichnisse) zu löschen, die älter als eine Woche sind. Aber ich bekomme%%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"

Antwort1

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.Keine gültige Maske in:/m *%%t

Der%%tist ein vollständiger Pfad, es ist ein resultierendes und zu 100 % qualifiziertes Element aus der vorherigen Schleife, aber keine Maske für die Verwendung durch forfiles, selbst wenn es zu hinzugefügt wird*, wird es nicht als Maske behandelt.

2.Sie können ersetzen/m *%%tZu:

/m "*.txt"

3.ForDurch Schleife ersetzen 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.Benutze einecho\FileNameum zu prüfen, ob es mit dem Namen der zu löschenden Datei übereinstimmt, indem Sie eine Umleitung |mitFindstrund der Operator&&( return 0) ausführendelwenn der Name übereinstimmt:

@echo\%~ni|findstr/bil localhost_access_log >nul && echo\ del /q /f "%~i"

5.Testen Sie, und bestätigen Sie, dass Sie zufrieden sind. Wenn ja, entfernen Sie echodie Datei, um sie wirksam zu löschen.

@echo\%~ni|findstr/bil localhost_access_log >nul && del /q /f "%~i"

Bem.:- Mit einer ForSchleife können Sie Ihre Variable erweitern:

    %~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

Zusätzliche Ressourcen:

verwandte Informationen