2

I have a directory with many temp files. All files older than 5 days can be deleted except one file 'DoNotDelete.txt'. Therefore I should add an extra argument to my current script:

 forfiles /p "D:\tmp" /s /d -5 /c "cmd /c del @file : date >= 5 days >NUL"

Can someone provide this argument?

2 Answers 2

0

I don't think forfiles has such an option (type 'forfiles /?'). As a workaround, you could (temporarily) set one of the system, read-only or hidden attributes on this file, e.g. attrib +h donotdelete.txt. This will prevent forfiles from deleting it.

3
0

I already have this VBScript to clean up this folder. How can I add the extra command to skip the 'DoNotDelete.txt' file? Can you help?

Regards, 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 
1
  • Thanx to Matthew Williams and Berend
    – Jvd
    Aug 29, 2014 at 14:18

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .