Jump to content

Folder Options - deleting files automatically


Recommended Posts

Guest Victor
Posted

Is there any way to automatically delete items in a particular folder that

are older than, say two weeks?

  • Replies 7
  • Created
  • Last Reply

Popular Days

Guest Pegasus \(MVP\)
Posted

Re: Folder Options - deleting files automatically

 

 

"Victor" <Victor@discussions.microsoft.com> wrote in message

news:EB518E2C-2A1C-48D9-8E8E-979144223694@microsoft.com...

> Is there any way to automatically delete items in a particular folder that

> are older than, say two weeks?

 

Yes, with a script that you launch with the Task Scheduler.

Guest Victor
Posted

Re: Folder Options - deleting files automatically

 

Thanks, but any chance you could provide me with a sample script?

 

"Pegasus (MVP)" wrote:

>

> "Victor" <Victor@discussions.microsoft.com> wrote in message

> news:EB518E2C-2A1C-48D9-8E8E-979144223694@microsoft.com...

> > Is there any way to automatically delete items in a particular folder that

> > are older than, say two weeks?

>

> Yes, with a script that you launch with the Task Scheduler.

>

>

>

Guest Pegasus \(MVP\)
Posted

Re: Folder Options - deleting files automatically

 

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" <Victor@discussions.microsoft.com> wrote in message

news:EB9A11F3-48F2-401F-8421-613C0AE0A449@microsoft.com...

> Thanks, but any chance you could provide me with a sample script?

>

> "Pegasus (MVP)" wrote:

>

>>

>> "Victor" <Victor@discussions.microsoft.com> wrote in message

>> news:EB518E2C-2A1C-48D9-8E8E-979144223694@microsoft.com...

>> > Is there any way to automatically delete items in a particular folder

>> > that

>> > are older than, say two weeks?

>>

>> Yes, with a script that you launch with the Task Scheduler.

>>

>>

>>

Guest Victor
Posted

Re: Folder Options - deleting files automatically

 

Thank you.

 

"Pegasus (MVP)" wrote:

> 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" <Victor@discussions.microsoft.com> wrote in message

> news:EB9A11F3-48F2-401F-8421-613C0AE0A449@microsoft.com...

> > Thanks, but any chance you could provide me with a sample script?

> >

> > "Pegasus (MVP)" wrote:

> >

> >>

> >> "Victor" <Victor@discussions.microsoft.com> wrote in message

> >> news:EB518E2C-2A1C-48D9-8E8E-979144223694@microsoft.com...

> >> > Is there any way to automatically delete items in a particular folder

> >> > that

> >> > are older than, say two weeks?

> >>

> >> Yes, with a script that you launch with the Task Scheduler.

> >>

> >>

> >>

>

>

>

Guest Pegasus \(MVP\)
Posted

Re: Folder Options - deleting files automatically

 

 

"Victor" <Victor@discussions.microsoft.com> wrote in message

news:2C378B4A-A3A8-43B4-A209-5ED1F9E9A90D@microsoft.com...

> Thank you.

>

 

You're welcome.

Guest Victor
Posted

Re: Folder Options - deleting files automatically

 

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.

 

"Pegasus (MVP)" wrote:

> 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" <Victor@discussions.microsoft.com> wrote in message

> news:EB9A11F3-48F2-401F-8421-613C0AE0A449@microsoft.com...

> > Thanks, but any chance you could provide me with a sample script?

> >

> > "Pegasus (MVP)" wrote:

> >

> >>

> >> "Victor" <Victor@discussions.microsoft.com> wrote in message

> >> news:EB518E2C-2A1C-48D9-8E8E-979144223694@microsoft.com...

> >> > Is there any way to automatically delete items in a particular folder

> >> > that

> >> > are older than, say two weeks?

> >>

> >> Yes, with a script that you launch with the Task Scheduler.

> >>

> >>

> >>

>

>

>

Guest Pegasus \(MVP\)
Posted

Re: Folder Options - deleting files automatically

 

You MUST do Steps 5 and 6 below!

 

"Victor" <Victor@discussions.microsoft.com> wrote in message

news:725D6A9C-96AE-43D3-8871-815B9847B0CC@microsoft.com...

> 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.

>

> "Pegasus (MVP)" wrote:

>

>> 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" <Victor@discussions.microsoft.com> wrote in message

>> news:EB9A11F3-48F2-401F-8421-613C0AE0A449@microsoft.com...

>> > Thanks, but any chance you could provide me with a sample script?

>> >

>> > "Pegasus (MVP)" wrote:

>> >

>> >>

>> >> "Victor" <Victor@discussions.microsoft.com> wrote in message

>> >> news:EB518E2C-2A1C-48D9-8E8E-979144223694@microsoft.com...

>> >> > Is there any way to automatically delete items in a particular

>> >> > folder

>> >> > that

>> >> > are older than, say two weeks?

>> >>

>> >> Yes, with a script that you launch with the Task Scheduler.

>> >>

>> >>

>> >>

>>

>>

>>


×
×
  • Create New...