GET-IT: TEAMS DAY | 1-Day Free Virtual Conference all about Teams. Here on Petri.com - 8/12/20 GET-IT: TEAMS DAY - 8/12/20

Help with script pulled from forum

Home Forums Scripting General Scripting Help with script pulled from forum

Viewing 1 post (of 1 total)
  • Author
    Posts
  • Avatar
    rhltechie
    Member
    #130031

    Hi All,

    I pulled a script from this forum that was written by JeremyW.

    ‘ ***************************************************
    ‘ * 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:Intel12.3”

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

    Subfolders = True

    ‘ * 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() – 0

    ‘ * Directory and filename for the logfile
    strDirectory = “c:logs”
    strFile = “fileDeleteLog.txt”

    strLogFile = strDirectory & “” & strFile

    ‘ *******************************************************
    ‘ * Check if the log file exists
    ‘ *******************************************************

    ‘ Create the File System Object
    Set objFSO = CreateObject(“Scripting.FileSystemObject”)

    ‘ Check that the strDirectory folder exists
    If objFSO.FolderExists(strDirectory) Then
    Set objFolder = objFSO.GetFolder(strDirectory)
    Else
    Set objFolder = objFSO.CreateFolder(strDirectory)
    End If

    If objFSO.FileExists(strDirectory & strFile) Then
    Set objFolder = objFSO.GetFolder(strDirectory)
    Else
    Set objFile = objFSO.CreateTextFile(strDirectory & strFile)
    End If

    set objFolder = nothing
    set objFile = nothing

    If err.number <> 0 then
    WScript.echo “VBScript Error: ” & err.number
    End If

    ‘ *******************************************************
    ‘ 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
    strFilePath = FilesArray(n).path
    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
    Set objFile = FSO.OpenTextFile(strLogFile, 8)
    objFile.WriteLine Now & vbTab & “ERROR: ” & Err.Number & ” ” & Err.Dscription & ” – Unable to delete: ” & strFilePath
    objFile.Close
    else
    numDeleted = numDeleted + 1
    Set objFile = FSO.OpenTextFile(strLogFile, 8)
    objFile.WriteLine Now & vbTab & “Deleted: ” & strFilePath
    objFile.Close

    end if
    ‘ Switch Error checking back to normal
    on error goto 0
    next

    Set objFile = FSO.OpenTextFile(strLogFile, 8)
    objFile.WriteBlankLines(1)
    objFile.WriteLine “Total Files and Folders Deleted: ” & numDeleted
    objFile.WriteBlankLines(4)
    objFile.Close

    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,FilesArray,true ' uses error trapping around access to the ' Date property just to be safe dtlastmodified = null on error resume Next dtlastmodified = fldr.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) = fldr end if end if next end if end sub[/CODE] It goes through a specified folder structure and deletes files and folders over a certain date. Can someone tell me how I might alter this script to delete the files only and not the subfolders? Also, I dont really need the date function, I am just running this script once a week to delete everything. If I enter 0 in the lifetime variable will this accomplish what I am after? TIA, R[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:Intel12.3”

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

    Subfolders = True

    ‘ * 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() – 0

    ‘ * Directory and filename for the logfile
    strDirectory = “c:logs”
    strFile = “fileDeleteLog.txt”

    strLogFile = strDirectory & “” & strFile

    ‘ *******************************************************
    ‘ * Check if the log file exists
    ‘ *******************************************************

    ‘ Create the File System Object
    Set objFSO = CreateObject(“Scripting.FileSystemObject”)

    ‘ Check that the strDirectory folder exists
    If objFSO.FolderExists(strDirectory) Then
    Set objFolder = objFSO.GetFolder(strDirectory)
    Else
    Set objFolder = objFSO.CreateFolder(strDirectory)
    End If

    If objFSO.FileExists(strDirectory & strFile) Then
    Set objFolder = objFSO.GetFolder(strDirectory)
    Else
    Set objFile = objFSO.CreateTextFile(strDirectory & strFile)
    End If

    set objFolder = nothing
    set objFile = nothing

    If err.number <> 0 then
    WScript.echo “VBScript Error: ” & err.number
    End If

    ‘ *******************************************************
    ‘ 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
    strFilePath = FilesArray(n).path
    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
    Set objFile = FSO.OpenTextFile(strLogFile, 8)
    objFile.WriteLine Now & vbTab & “ERROR: ” & Err.Number & ” ” & Err.Dscription & ” – Unable to delete: ” & strFilePath
    objFile.Close
    else
    numDeleted = numDeleted + 1
    Set objFile = FSO.OpenTextFile(strLogFile, 8)
    objFile.WriteLine Now & vbTab & “Deleted: ” & strFilePath
    objFile.Close

    end if
    ‘ Switch Error checking back to normal
    on error goto 0
    next

    Set objFile = FSO.OpenTextFile(strLogFile, 8)
    objFile.WriteBlankLines(1)
    objFile.WriteLine “Total Files and Folders Deleted: ” & numDeleted
    objFile.WriteBlankLines(4)
    objFile.Close

    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,FilesArray,true

    ‘ uses error trapping around access to the
    ‘ Date property just to be safe
    dtlastmodified = null
    on error resume Next
    dtlastmodified = fldr.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) = fldr
    end if
    end if
    next
    end if
    end sub[/CODE]

    It goes through a specified folder structure and deletes files and folders over a certain date. Can someone tell me how I might alter this script to delete the files only and not the subfolders? Also, I dont really need the date function, I am just running this script once a week to delete everything. If I enter 0 in the lifetime variable will this accomplish what I am after?

    TIA,

    R

Viewing 1 post (of 1 total)

You must be logged in to reply to this topic.