V
Victor
Is there any way to automatically delete items in a particular folder that
are older than, say two weeks?
are older than, say two weeks?
Follow along with the video below to see how to install our site as a web app on your home screen.
Note: This feature may not be available in some browsers.
Victor said:Is there any way to automatically delete items in a particular folder that
are older than, say two weeks?
Pegasus (MVP) said:You can try the script below. Here is what you need to do:
1. Copy & paste the script into c:\Windows\DelOldFiles.vbs
2. Adjust Lines #7, 8 and 9 to suit your requirements.
3. Unwrap the lines that may have been wrapped around by your newsreader.
4. Remove all line numbers.
5. Start a Command Prompt (click Start / Run / cmd {OK})
6. Type this command to test the script.
cscript //nologo c:\windows\DelOldFiles.vbs {Enter}
7. Examine the screen output very carefully.
8. If you're satisfied with the result, set Active = True in Line #6.
9. Test the script again.
10. Schedule it to run once each day.
01. '--------------------------------------------
02. 'This script will delete files that were last
03. 'updated "MaxAge" days ago.
04. '21.8.2008 FNL
05. '--------------------------------------------
06. Const Active = False
07. Const sSource = "d:\Temp\"
08. Const MaxAge = 10 'days
09. Const Recursive = True 'Recurse into subdirectories
10.
11. Checked = 0
12. Deleted = 0
13. Would = 0
14.
15. Set objFSO = CreateObject("Scripting.FileSystemObject")
16. CheckFolder sSource
17. WScript.echo
18. If Active Then
19. WScript.Echo Checked & " file(s) checked, " & Deleted & " file(s)
deleted."
20. Else
21. WScript.Echo Checked & " file(s) checked, " & Would & " file(s) would
be deleted."
22. End If
23.
24. Sub CheckFolder (sWD)
25. Set objFolder = objFSO.GetFolder(sWD)
26. Set objFiles = objFolder.Files
27. For Each objFile In objFiles
28. Checked = Checked + 1
29. If DateDiff("D", objFile.DateLastModified, Now()) > MaxAge Then
30. If Active Then
31. WScript.Echo "Deleting """ & sWD & objFile.Name & """"
32. objFile.Delete
33. Deleted = Deleted + 1
34. Else
35. WScript.Echo "Old file: """ & sWD & objFile.Name & """"
36. Would = Would + 1
37. End If
38. End If
39. Next
40.
41. if not Recursive then Exit Sub
42. Set objFolders = objFolder.SubFolders
43. For Each objFolder In objFolders
44. CheckFolder(sWD & objFolder.Name & "\")
45. Next
46. End Sub
Victor said:Thank you.
Victor said:I've just run the script, but the small dos window goes by too fast to see
anything. How can I have it stay on the screen? Thanks.
Want to reply to this thread or ask your own question?
You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.