Exclude Specified File/Folder Names (VBS)

Home Forums Scripting General Scripting Exclude Specified File/Folder Names (VBS)

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

    Following on from this where we ended up the following which will search in a specified folder directory on a remote machine and delete files based on age…

    (set to 10 days at the moment) – tested and works fine. But simply I wondered if it were possible to exclude specific folders from a list…

    For Example;
    delete all the files/folders over 10 days old in ‘Folder1’ and ‘Folder2’ but not from ‘Folder1Important Docs’ – ’cause that’d be silly…. :)

    I’m guessing I might have to define a list of words for it to ‘skip’ as it searches…

    ‘ ***************************************************
    ‘ * 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.
    ‘ Input for remote machine

    RemPath = InputBox(“Enter Computer Name or drive to clean:”, “RemoteLocal Cleanup”)

    If Len(RemPath) = 1 OR (Len(RemPath) = 2 AND Right(RemPath, 1) = “:”) Then
    If Len(RemPath) = 1 Then
    strRoot = RemPath & “:”
    Else
    strRoot = RemPath & “”
    End If
    RemPath = “\local”

    ElseIf NOT Left(RemPath,2) = “\” Then
    RemPath = “\” & RemPath
    strRoot = RemPath & “c$”
    Else
    Wscript.Echo “Something’s wrong… try running the script again”
    Wscript.Quit
    End If

    ‘———————Add array here—————————-
    Dim arrDirPaths()
    intSize = 0
    intLoop = 0

    Do Until intLoop = 1
    strDirPath = InputBox(“Please enter the directory you want to clean.” & VbCrLF _
    & “e.g. ‘windowstemp'” & VbCrLF & VbCrLF & “If you have all the diretories entered then ” _
    & “leave the box blank and click OK.”, “Directory Path”)
    If NOT strDirPath = “” Then
    ReDim Preserve arrDirPaths(intSize)
    arrDirPaths(intSize) = strDirPath
    intSize = intSize + 1
    Else
    intDialogBox = Msgbox(“Are you ready to proceed?” & vbCrLF & vbCrLF & _
    “Yes to continue” & vbCrLF & “No to enter more paths” _
    & vbCrLF & “Cancel to exit the script” ,3,”Path Input…?”)
    If intDialogBox = 2 Then
    Wscript.Quit
    ElseIf intDialogBox = 6 Then
    intLoop = 1
    End If
    End If
    Loop
    ‘————–add check before proceeding—————-
    If intSize = 0 Then
    Wscript.Echo “No directory was entered. The script will now quit”
    Wscript.quit
    End If

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

    ‘ * Change the path to where the log file is.
    ‘ * Be sure the text file already exists or else
    ‘ * script will fail. Change this to RemPath & “c$logs” to store on remote machine
    strLogFile = “c:logs” & Right(RemPath, Len(RemPath) – 2) & “_fileDeleteLog.txt”

    ‘ *******************************************************
    ‘ * Check if the log file exists
    ‘ *******************************************************
    strDirectory = “c:logs”
    strFile = “” & Right(RemPath, Len(RemPath) – 2) & “_fileDeleteLog.txt”

    ‘ 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)
    WScript.Echo “Just created ” & strDirectory
    End If

    If objFSO.FileExists(strDirectory & strFile) Then
    Set objFolder = objFSO.GetFolder(strDirectory)
    Else
    Set objFile = objFSO.CreateTextFile(strDirectory & strFile)
    Wscript.Echo “Just created ” & strDirectory & strFile
    End If

    set objFolder = nothing
    set objFile = nothing

    If err.number = vbEmpty then
    Set objShell = CreateObject(“WScript.Shell”)
    objShell.run (“Explorer” & ” ” & strDirectory & “” )
    Else WScript.echo “VBScript Error: ” & err.number
    End If

    numDeleted = 0

    ‘———————Add “For Each” loop here—————————-

    For i = 0 to intSize -1
    Path = strRoot & arrDirPaths(i)

    ‘ 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

    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
    ‘100 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

    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 fso = createobject(“scripting.filesystemobject”)
    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][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.
    ‘ Input for remote machine

    RemPath = InputBox(“Enter Computer Name or drive to clean:”, “RemoteLocal Cleanup”)

    If Len(RemPath) = 1 OR (Len(RemPath) = 2 AND Right(RemPath, 1) = “:”) Then
    If Len(RemPath) = 1 Then
    strRoot = RemPath & “:”
    Else
    strRoot = RemPath & “”
    End If
    RemPath = “\local”

    ElseIf NOT Left(RemPath,2) = “\” Then
    RemPath = “\” & RemPath
    strRoot = RemPath & “c$”
    Else
    Wscript.Echo “Something’s wrong… try running the script again”
    Wscript.Quit
    End If


    Add array here


    Dim arrDirPaths()
    intSize = 0
    intLoop = 0

    Do Until intLoop = 1
    strDirPath = InputBox(“Please enter the directory you want to clean.” & VbCrLF _
    & “e.g. ‘windowstemp'” & VbCrLF & VbCrLF & “If you have all the diretories entered then ” _
    & “leave the box blank and click OK.”, “Directory Path”)
    If NOT strDirPath = “” Then
    ReDim Preserve arrDirPaths(intSize)
    arrDirPaths(intSize) = strDirPath
    intSize = intSize + 1
    Else
    intDialogBox = Msgbox(“Are you ready to proceed?” & vbCrLF & vbCrLF & _
    “Yes to continue” & vbCrLF & “No to enter more paths” _
    & vbCrLF & “Cancel to exit the script” ,3,”Path Input…?”)
    If intDialogBox = 2 Then
    Wscript.Quit
    ElseIf intDialogBox = 6 Then
    intLoop = 1
    End If
    End If
    Loop


    add check before proceeding


    If intSize = 0 Then
    Wscript.Echo “No directory was entered. The script will now quit”
    Wscript.quit
    End If

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

    ‘ * Change the path to where the log file is.
    ‘ * Be sure the text file already exists or else
    ‘ * script will fail. Change this to RemPath & “c$logs” to store on remote machine
    strLogFile = “c:logs” & Right(RemPath, Len(RemPath) – 2) & “_fileDeleteLog.txt”

    ‘ *******************************************************
    ‘ * Check if the log file exists
    ‘ *******************************************************
    strDirectory = “c:logs”
    strFile = “” & Right(RemPath, Len(RemPath) – 2) & “_fileDeleteLog.txt”

    ‘ 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)
    WScript.Echo “Just created ” & strDirectory
    End If

    If objFSO.FileExists(strDirectory & strFile) Then
    Set objFolder = objFSO.GetFolder(strDirectory)
    Else
    Set objFile = objFSO.CreateTextFile(strDirectory & strFile)
    Wscript.Echo “Just created ” & strDirectory & strFile
    End If

    set objFolder = nothing
    set objFile = nothing

    If err.number = vbEmpty then
    Set objShell = CreateObject(“WScript.Shell”)
    objShell.run (“Explorer” & ” ” & strDirectory & “” )
    Else WScript.echo “VBScript Error: ” & err.number
    End If

    numDeleted = 0


    Add “For Each” loop here


    For i = 0 to intSize -1
    Path = strRoot & arrDirPaths(i)

    ‘ 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

    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
    ‘100 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

    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 fso = createobject(“scripting.filesystemobject”)
    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]

Viewing 1 post (of 1 total)

You must be logged in to reply to this topic.