Solved

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

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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Stuck in voice control mode on your Amazon Firestick?  Here is how to turn it off!!!
Whether you believe the “gig economy,” as it has been dubbed, is the next big economic paradigm shift (https://www.theguardian.com/commentisfree/2015/jul/26/will-we-get-by-gig-economy) or an overstated trend (http://www.wsj.com/articles/proof-of-a-g…
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…
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…

856 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