VBScript to delete 7yo files

August 11, 2011 at 11:07:14
Specs: Windows XP
New to scripting...

I have the following script below to delete users 'My Documents' files older than seven years. I am wondering how I would user the %username% field to correctly point to the users My Documents folder.

'-------------- CleanUp.vbs --------------------
Option Explicit
Dim fso
Set fso = CreateObject("Scripting.FileSystemObject")
DeleteFiles fso.GetFolder("C:\Documents and Settings\%username%\My Documents")
Sub DeleteFiles(srcFolder)
Dim srcFile
If srcFolder.Files.Count = 0 Then
Exit Sub
End If
For Each srcFile in srcFolder.Files
If DateDiff("d", Now, srcFile.DateCreated) < -2555 Then
fso.DeleteFile srcFile, True
End If
End Sub

Thanks for your help!

See More: VBScript to delete 7yo files

Report •

August 11, 2011 at 12:06:21
Where'd you get this? This breaks on any non-English install, on any non-standard setup, breaks on Vista / Win7, and it doesn't account for leap years. It also uses a useless Sub, that looks like it was at one time intended to do recursive searches, but the programmer never got around to it. It also assumes a file created 7 years ago is never referenced again, but that's probably more due to the spec than the programmer. Not accessed for 7 years would be a safer option, but this assumes nothing changes the accessed date by itself (like a virus scanner or backup program).
Final Score: 2 / 5

Better script:

Set fso = CreateObject("Scripting.FileSystemObject")
For Each srcFile In fso.GetFolder( _
  If DateDiff("d", srcFile.DateLastAccessed, Now) > 365.25 * 7 Then _
    srcFile.DeleteFile True

How To Ask Questions The Smart Way

Report •

August 11, 2011 at 12:54:03
Thanks for the assistance Razor2.3! Sorry, I should have given more detail in the initial post...

I found the original version at this link: http://forums.aspfree.com/windows-s...

The script we are after needs to only delete files that have a created date of 7 years or more (due to a new data retention policy being implemented). We don't care about the modified date. No folder deletion needs to take place, only files in "My Documents" and any subfolders of this folder.

Report •

August 11, 2011 at 18:41:41
See, the original doesn't have any of those problems, except the half-done recursion which we need to finish, and the whole going by date created thing, which is in the specs, so not much we can do about that.
Set fso = CreateObject("Scripting.FileSystemObject")
DelFiles fso.GetFolder( _

Sub DelFiles(oDir)
  On Error Resume Next
  For Each srcFile In oDir.Files
    If DateDiff("d", srcFile.DateCreated, Now) > 365.25 * 7 Then _
      srcFile.DeleteFile True
  Next 'scrFile
  For Each dir In oDir.Subfolders
    DelFiles dir
  Next 'dir
End Sub

How To Ask Questions The Smart Way

Report •

Related Solutions

Ask Question