Solved

VBS to Folders Older and Exclude Specified Folder to 1 Subfolder Down from Root

Posted on 2013-12-12
5
249 Views
Last Modified: 2014-05-19
I am after a script that will delete all files and folders older than a specified number of days which the user inputs, but exclude certain folders and subfolder from deletion.

Path the script would run:

Root Folder: C:\inetpub\ftproot\LocalUser\
The root has hundreds of subfolders which I dont want deleted.

It should look into root folder, then scan each folder within it a delete the contents of that folder, but not the parent within the root folder, this would delete everything that is of a specified number of days.

I want to exclude these folders from the script so the content does not get deleted.
C:\inetpub\ftproot\LocalUser\PUBLIC
C:\inetpub\ftproot\LocalUser\TEMP

I have found many different versions, but they all relate to a singular folder.
Now I know I can add a "globaldepthcount" so i can specify the depth from the root folder..

ie:
dim GlobalDepthCount
GlobalDepthCount = 1
includeDepths = Array(GlobalDepthCount)', "2")

Then adding the rest of the code as a sub rountine..
But not being brilliant on scripting, I keep getting my subroutines in a mix..

So any help would be greatly appreciated.,

Essentially, all I want to do is run the script as a scheduled task, to clearout FTP Data from all ftp user account home folders, that being files and folders but exclude staff folders from being affected..
0
Comment
Question by:ExcitechSupport
  • 3
  • 2
5 Comments
 
LVL 26

Expert Comment

by:MacroShadow
Comment Utility
I have not tested this but it (or something like it) should work.

Option Explicit

Dim fso, FileSet, Path, File, DDiff
Dim strDir, strSkip1, strSkip2
Dim ObjFolder, ObjSubFolders, ObjSubFolder, FolderSet, Folder

strDir = "C:\inetpub\ftproot\LocalUser\"
strSkip1 = "C:\inetpub\ftproot\LocalUser\PUBLIC"
strSkip2 = "C:\inetpub\ftproot\LocalUser\TEMP"

Set fso = CreateObject("Scripting.FileSystemObject")
Set ObjFolder = fso.GetFolder(strDir)
Set ObjSubFolders = ObjFolder.SubFolders

For Each ObjSubFolder In ObjSubFolders
    If ObjSubFolder.Path <> strSkip1 And ObjSubFolder.Path <> strSkip2 Then
        FileSet = GetDirFiles(strDir)

        For Each File In FileSet
            Set File = fso.GetFile(File)
            
            DDiff = Abs(DateDiff("d", File.DateLastModified, Now()))

            If DDiff >= 30 Then
'                Kill File
                File.Delete
            End If
        Next
    End If
Next

Function GetDirFiles(FolderPath)
    Dim FileCollection, aTmp(), i
    Set fso = CreateObject("Scripting.FileSystemObject")
    Set FileCollection = fso.GetFolder(FolderPath).Files

    ReDim aTmp(FileCollection.Count - 1)
    i = -1

    For Each File In FileCollection
        i = i + 1
        aTmp(i) = File.Path
    Next

    GetDirFiles = aTmp
End Function

Open in new window

0
 
LVL 26

Expert Comment

by:MacroShadow
Comment Utility
Any progress?
0
 

Author Comment

by:ExcitechSupport
Comment Utility
Thanks very much, i will test in a few days. Had a brief look at the script, but nothing further as of yet...
0
 

Author Comment

by:ExcitechSupport
Comment Utility
I have tried the script and adjusted some of it to test on a virtual, however even reducing the LastDateModified to 1 day, no data is removed.

I am in process of looking at a few examples I had created and merging them, but this is beginning to be a little messy...

Any other suggestions?
0
 
LVL 26

Accepted Solution

by:
MacroShadow earned 500 total points
Comment Utility
Did you un-comment line 25?
0

Featured Post

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

If you get continual lockouts after changing your Active Directory password, there are several possible reasons.  Two of the most common are using other devices to access your email and stored passwords in the credential manager of windows.
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
Articles on a wide range of technology and professional topics are available on Experts Exchange. These resources are written by members, for members, and can be written about any topic you feel passionate about. Learn how to best write an article t…
Saved searches can save you time by quickly referencing commonly searched terms on any topic. Whether you are looking for questions you can answer or hoping to learn about a specific issue, a saved search can help you get the most out of your time o…

743 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now