Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

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

Posted on 2013-12-12
5
Medium Priority
?
261 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 2000 total points
ID: 39768442
Did you un-comment line 25?
0

Featured Post

Enroll in September's Course of the Month

This month’s featured course covers 16 hours of training in installation, management, and deployment of VMware vSphere virtualization environments. It's free for Premium Members, Team Accounts, and Qualified Experts!

Question has a verified solution.

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

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…
In this article, I’ll show how research, determination, and use of modern technology helped me solve a DNA mystery.
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.
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…

722 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