Delete files older then a number of days

Reading Time: < 1 minute

If you wish to delete files older then a certain number of days, copy the code below and paste to a notepad file. Next, save the file with an extension of .vbs e.g. delete.vbs

Dim fso, f, f1, fc
Set fso = CreateObject(“Scripting.FileSystemObject”)
Set f = fso.GetFolder(“C:\WINDOWS\Temp”)
Set fc = f.Files
For Each f1 in fc
If DateDiff(“d”, f1.DateLastModified, Now) > 30 Then f1.Delete
Next

 

You can amend the file path on line 3 and the number of days on line 6. Currently the script is set to delete files from C:\WINDOWS\Temp which are older then 30 days.

I would highly recommend that you test the script before applying to a live environment. Once happy, you can run manually or add to a scheduled task to run automatically.

Please note: you will not be prompted to confirm yes or no, before the files are deleted.

Please follow and like us: