Solved

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

Posted on 2013-12-12
5
252 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 27

Expert Comment

by:MacroShadow
ID: 39713952
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 27

Expert Comment

by:MacroShadow
ID: 39756619
Any progress?
0
 

Author Comment

by:ExcitechSupport
ID: 39758918
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
ID: 39768144
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 27

Accepted Solution

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

Featured Post

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Learn more about the importance of email disclaimers with our top 10 email disclaimer DOs and DON’Ts.
EE introduced a new rating method known as Level, which displays in your avatar as LVL. The new Level is a numeric ranking that is based on your Points. This article discusses the rationale behind the new method and provides the mathematical formula…
The Bounty Board allows you to request an article or video on any technical topic, or fulfill a bounty request to earn points. Watch this video to learn how to use the Bounty Board to get the content you want, earn points, and browse submitted bount…
Notifications on Experts Exchange help you keep track of your activity and updates in one place. Watch this video to learn how to use them on the site to quickly access the content that matters to you.

777 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