Solved

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

Posted on 2013-12-12
5
256 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 500 total points
ID: 39768442
Did you un-comment line 25?
0

Featured Post

Enroll in May's Course of the Month

May’s Course of the Month is now available! Experts Exchange’s Premium Members and Team Accounts have access to a complimentary course each month as part of their membership—an extra way to increase training and boost professional development.

Question has a verified solution.

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

CSS is a visual language used to classify objects and define rules about how they should be displayed. CSS skills aren’t restricted to developers anymore, there is a big benefit to having a basic understanding of the language, regardless of your occ…
When you see single cell contains number and text, and you have to get any date out of it seems like cracking our heads.
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…
Where to go on the main page to find the job listings. How to apply to a job that you are interested in from the list that is featured on our Careers page.

710 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