Deleting Temp Files in User Profiles with VBS

Home Forums Scripting General Scripting Deleting Temp Files in User Profiles with VBS

Viewing 1 post (of 1 total)
  • Author
    Posts
  • Avatar
    Lan
    Member
    #126268

    Good day all,

    Just wondering… well to be honest it’s bound to be possible – I just don’t know how complex…

    Anyway, back to the question at hand… basically I’d like to have a vbs script that will enable me to clear out temporary files in a user profile based on date…

    e.g. everything in: c:documents and settingsuserlocal settingstemporary internet files and everything in c:documents and settingsuserlocal settingstemp that’s over x days old…

    I have been given one that deletes files in c:winxpprotemp over 20 days old by a friend (but its probably floating around the internet) – see below:

    ‘ ***************************************************
    ‘ * This script will delete all files in a folder and
    ‘ * sub-folder where when files are x number of days
    ‘ * old.
    ‘ *
    ‘ * Files will be deleted and not retained in the
    ‘ * recycle bin.
    ‘ *
    ‘ * Alter the path and lifetime variables to your needs

    ‘ * Alter this variable, this is the starting point
    ‘ * for the deletion.
    ‘ * Example : path = “\teacherbackup”
    ‘ * Example : path = “N:Logfiles”

    Path = “C:WINXPProTEMP”

    ‘ * Alter this variable, if sub-folders should be processed
    ‘ * Process Sub folders Example : Subfolders = True
    ‘ * Process Sub folders Example : Subfolders = False

    Subfolders = False
    ‘ * Alter this variable to set the how many days old
    ‘ * the file should be before it is deleted.

    ‘ * Example : lifetime = date() – 20
    ‘ * Will delete files that are 20 days old or more

    Lifetime = date() – 20

    ‘ *******************************************************
    ‘ Script starts at this point
    ‘ *******************************************************
    ‘ Declare a variable as an Array that will store a listing
    ‘ of files that will be checked.

    FilesArray = Array()

    ‘ Create an instance of the FileSystemObject so that file
    ‘ information can be obtained.

    set fso = createobject(“scripting.filesystemobject”)

    ‘ Call the SelectFiles procedure to Fill the array with
    ‘ files and folders that will be deleted

    SelectFiles path, lifetime, FilesArray, Subfolders

    ‘ Process the FilesArray deleting files as we loop through

    numDeleted = 0

    for n = 0 to ubound(FilesArray)
    ‘ Switch off error checking, so that errors are ignored
    on error resume next
    ‘ Call the delete function to delete the selected file
    FilesArray(n).delete true
    ‘ Handle any errors or results. This could be modified
    ‘ to log to a text file.
    ‘if err.number <> 0 then
    ‘ wscript.echo “Unable to delete: ” & FilesArray(n).path
    ‘else
    ‘ numDeleted = numDeleted + 1
    ‘end if
    ‘ Switch Error checking back to normal
    on error goto 0
    next

    sub SelectFiles(sPath,vlifetime,FilesArrayToKill,bIncludeSubFolders)
    ‘ Switch off Error handling, errors ignored.
    on error resume next
    ‘select files to delete and add to array…
    set folder = fso.getfolder(sPath)
    set files = folder.files

    ‘ Loop through files that have been found
    for each file in files
    ‘ uses error trapping around access to the
    ‘ Date property just to be safe

    dtlastmodified = null
    on error resume Next
    dtlastmodified = file.datelastmodified
    on error goto 0

    if not isnull(dtlastmodified) Then
    if dtlastmodified < vlifetime then count = ubound(FilesArrayToKill) + 1 redim preserve FilesArrayToKill(count) set FilesArrayToKill(count) = file end if end if next ' If sub-folders are selected, call the procedure again to update ' the array with the contents. if bIncludeSubFolders then for each fldr in folder.subfolders SelectFiles fldr.path,vlifetime,FilesArrayToKill,true next end if end sub[/CODE] To be perfectly honest I'm not hugely familar with half the things in there but am trying to go through and understand it... After its deleted all those I'd like it to also report back to say what its deleted - a log file or msgbox... I imagine it'd be a huge amount so probably a log file would be best. so... any takers? :bowdown:[CODE]' ***************************************************
    ‘ * This script will delete all files in a folder and
    ‘ * sub-folder where when files are x number of days
    ‘ * old.
    ‘ *
    ‘ * Files will be deleted and not retained in the
    ‘ * recycle bin.
    ‘ *
    ‘ * Alter the path and lifetime variables to your needs

    ‘ * Alter this variable, this is the starting point
    ‘ * for the deletion.
    ‘ * Example : path = “\teacherbackup”
    ‘ * Example : path = “N:Logfiles”

    Path = “C:WINXPProTEMP”

    ‘ * Alter this variable, if sub-folders should be processed
    ‘ * Process Sub folders Example : Subfolders = True
    ‘ * Process Sub folders Example : Subfolders = False

    Subfolders = False
    ‘ * Alter this variable to set the how many days old
    ‘ * the file should be before it is deleted.

    ‘ * Example : lifetime = date() – 20
    ‘ * Will delete files that are 20 days old or more

    Lifetime = date() – 20

    ‘ *******************************************************
    ‘ Script starts at this point
    ‘ *******************************************************
    ‘ Declare a variable as an Array that will store a listing
    ‘ of files that will be checked.

    FilesArray = Array()

    ‘ Create an instance of the FileSystemObject so that file
    ‘ information can be obtained.

    set fso = createobject(“scripting.filesystemobject”)

    ‘ Call the SelectFiles procedure to Fill the array with
    ‘ files and folders that will be deleted

    SelectFiles path, lifetime, FilesArray, Subfolders

    ‘ Process the FilesArray deleting files as we loop through

    numDeleted = 0

    for n = 0 to ubound(FilesArray)
    ‘ Switch off error checking, so that errors are ignored
    on error resume next
    ‘ Call the delete function to delete the selected file
    FilesArray(n).delete true
    ‘ Handle any errors or results. This could be modified
    ‘ to log to a text file.
    ‘if err.number <> 0 then
    ‘ wscript.echo “Unable to delete: ” & FilesArray(n).path
    ‘else
    ‘ numDeleted = numDeleted + 1
    ‘end if
    ‘ Switch Error checking back to normal
    on error goto 0
    next

    sub SelectFiles(sPath,vlifetime,FilesArrayToKill,bIncludeSubFolders)
    ‘ Switch off Error handling, errors ignored.
    on error resume next
    ‘select files to delete and add to array…
    set folder = fso.getfolder(sPath)
    set files = folder.files

    ‘ Loop through files that have been found
    for each file in files
    ‘ uses error trapping around access to the
    ‘ Date property just to be safe

    dtlastmodified = null
    on error resume Next
    dtlastmodified = file.datelastmodified
    on error goto 0

    if not isnull(dtlastmodified) Then
    if dtlastmodified < vlifetime then
    count = ubound(FilesArrayToKill) + 1
    redim preserve FilesArrayToKill(count)
    set FilesArrayToKill(count) = file
    end if
    end if
    next

    ‘ If sub-folders are selected, call the procedure again to update
    ‘ the array with the contents.

    if bIncludeSubFolders then
    for each fldr in folder.subfolders
    SelectFiles fldr.path,vlifetime,FilesArrayToKill,true
    next
    end if
    end sub[/CODE]

    To be perfectly honest I’m not hugely familar with half the things in there but am trying to go through and understand it…

    After its deleted all those I’d like it to also report back to say what its deleted – a log file or msgbox… I imagine it’d be a huge amount so probably a log file would be best.

    so… any takers? :bowdown:

Viewing 1 post (of 1 total)

You must be logged in to reply to this topic.