Computing.Net > Forums > Programming > vbs script to delete many user subfolders

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

vbs script to delete many user subfolders

Reply to Message Icon

Name: Zoobaby
Date: May 17, 2009 at 04:52:41 Pacific
OS: Windows XP
Subcategory: General
Comment:

Hello everyone, first post, and not any easy one..

I currently have a script from a few examples that I mashed together that works wonderfully to delete folders and it's contents based on a date older than X. Great for use on a login script, but slows down the users log in time.

I need this same script to do the same thing for many user accounts (in place of strUserName) or possibly a loop of the same script changing the strUserName focus.
example:
C:\Documents and Settings\User1\cookies
C:\Documents and Settings\User2\cookies
C:\Documents and Settings\User3\cookies
C:\Documents and Settings\User4\cookies

Here is a copy of what I currently have:

Dim objNetwork
Dim strFolderLocation, strUserName, strTargetFolder

' gets name of local user account for use in path
Set objNetwork = WScript.CreateObject("WScript.Network")
strUserName = objNetwork.UserName

' name of folder location (not including username)
strFolderLocation = "C:\Documents and Settings\"

' name of target folder (after username folder)
strTargetFolder = "\cookies"

' set to false for testing purposes
Const Active = true

'number of days old you would like to filter
Const MaxAge = 2 'days

Const Recursive = true
Checked = 0
Deleted = 0

Set oFSO = CreateObject("Scripting.FileSystemObject")
if active then verb = "Deleting """ Else verb = "Old file: """

''CheckFolder oFso.GetFolder(strFolderLocation & strUserName & strTargetFolder)
CheckFolder oFso.GetFolder(strFolderLocation)

' *** Mark out the below line to prevent the script prompting you for each file/folder ***
' WScript.echo

if Active then verb = " file(s) deleted" Else verb = " file(s) would be deleted"

' *** Mark out the below line to prevent the script prompting you for each file/folder ***
' WScript.Echo Checked & " file(s) checked, " & Deleted & verb


Sub CheckFolder (oFldr)
For Each oFile In oFldr.Files
Checked = Checked + 1
If DateDiff("D", oFile.DateLastModified, Now()) > MaxAge Then
Deleted = Deleted + 1

' *** Mark out the below line to prevent the script prompting you for each file/folder ***
' WScript.Echo verb & oFile.Path & """"

If Active Then oFile.Delete
End If
Next

if not Recursive then Exit Sub
For Each oSubfolder In oFldr.Subfolders
CheckFolder(oSubfolder)
Next
End Sub


Any ideas would be greatly appreciated!
Z



Sponsored Link
Ads by Google
Reply to Message Icon

Related Posts

See More







Post Locked

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.


Go to Programming Forum Home


Sponsored links

Ads by Google


Results for: vbs script to delete many user subfolders

VBS script to delete files www.computing.net/answers/programming/vbs-script-to-delete-files/14805.html

VB Script for deleting files www.computing.net/answers/programming/vb-script-for-deleting-files-/16904.html

VB script to put time and date on www.computing.net/answers/programming/vb-script-to-put-time-and-date-on/18241.html