批处理文件,用于删除超过10天的文件夹中的所有文本文件,但某些文件除外(Batch file to delete all text files in a folder over 10 days old except certain ones)

我是批处理文件的新手,尝试编写一个将删除10天以上文件夹中所有.txt文件的文件,除了一个名为template.txt的文件。 这是怎么做到的? 我有以下但它删除所有txt文件超过10天。 感谢你的帮助。

forfiles /p "C:\test" /s /m *.txt /c "cmd /c del @path" /d -10

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

最满意答案

只需将forition实现到由forfiles运行的命令行中,如下所示:

forfiles /S /P "C:\test" /M "*.txt" /D -10 /C "cmd /C if @isdir==FALSE if /I not @file==0x22template.txt0x22 del @path"
 

if @isdir==FALSE部分是为了排除任何目录被进一步处理,以防其名称末尾有一些.txt (虽然不太可能),因为forfiles枚举了文件和目录。

if /I not @file==0x22template.txt0x22变为if /I not "<name of currently iterated item>"=="template.txt"并排除名为template.txt文件被删除。 /I选项使比较不区分大小写,例如Windows也会处理文件和目录路径。

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.

更多推荐