我有一个包含许多临时文件的目录。除“DoNotDelete.txt”文件外,所有超过 5 天的文件都可以删除。因此,我应该在当前脚本中添加一个额外的参数:
forfiles /p "D:\tmp" /s /d -5 /c "cmd /c del @file : date >= 5 days >NUL"
有人可以提供这个论点吗?
答案1
我认为 forfiles 没有这样的选项(输入“forfiles /?”)。作为一种解决方法,您可以(临时)为此文件设置系统、只读或隐藏属性之一,例如attrib +h donotdelete.txt
。这将阻止 forfiles 删除它。
答案2
我已经有了此 VBScript 来清理此文件夹。我该如何添加额外的命令来跳过“DoNotDelete.txt”文件?您能帮忙吗?
问候,JvD
Dim fso, startFolder, OlderThanDate
Set fso = CreateObject("Scripting.FileSystemObject")
startFolder = "D:\tmp" ' folder to start deleting (subfolders will also be cleaned)
OlderThanDate = DateAdd("d", -04, Date) ' 04 days (adjust as necessary)
DeleteOldFiles startFolder, OlderThanDate
Function DeleteOldFiles(folderName, BeforeDate)
Dim folder, file, fileCollection, folderCollection
Set folder = fso.GetFolder(folderName)
Set fileCollection = folder.Files
For Each file In fileCollection
If file.DateLastModified < BeforeDate And StrComp(file.name,'donotdelete.txt', vbTextCompare) <> 0 Then
fso.DeleteFile(file.Path)
End If