![FORFILES im Batch, um bestimmte Suffixdateien zu löschen, die älter als eine Woche sind](https://rvso.com/image/1646052/FORFILES%20im%20Batch%2C%20um%20bestimmte%20Suffixdateien%20zu%20l%C3%B6schen%2C%20die%20%C3%A4lter%20als%20eine%20Woche%20sind.png)
Ich möchte FORFILES im Batch verwenden, um NUR txt- localhost_access_log
Dateien (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%%t
ist 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 *%%t
Zu:
/m "*.txt"
3.For
Durch 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\FileName
um zu prüfen, ob es mit dem Namen der zu löschenden Datei übereinstimmt, indem Sie eine Umleitung |
mitFindstr
und der Operator&&
( return 0
) ausführendel
wenn 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 echo
die Datei, um sie wirksam zu löschen.
@echo\%~ni|findstr/bil localhost_access_log >nul && del /q /f "%~i"
Bem.:- Mit einer For
Schleife 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:
Del /?
Echo /?
For /?
For /F /?
Forfiles /?
- Umleitung
|
,<
,>
,2>
, usw.
- Bedingte Ausführung || && ...