I'm new to batch files, trying to write one that will delete all .txt files in a folder over 10 days old EXCEPT one called template.txt. How is this done? I have the below but it deletes ALL txt files over 10 days. Appreciate your help.
forfiles /p "C:\test" /s /m *.txt /c "cmd /c del @path" /d -10
Just implement the contition into the command line run by forfiles
, like this:
forfiles /S /P "C:\test" /M "*.txt" /D -10 /C "cmd /C if @isdir==FALSE if /I not @file==0x22template.txt0x22 del @path"
The if @isdir==FALSE
part is to exclude any directories from being processed further in case there are some with .txt
at the end of their names (although quite unlikely), because forfiles
enumerates both files and directories.
if /I not @file==0x22template.txt0x22
becomes if /I not "<name of currently iterated item>"=="template.txt"
and excludes files named template.txt
from being deleted. The /I
option makes the comparison case-insensitive, like Windows also treats file and directory paths.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.