VB script to delete folders older than 7 days

Hello,

I would like to create a VB script to automatically clears the folders on a shared drive older than 7 days. I tried different codes but nothing deletes the folder all i can do is delete the files.
But i have multiple folder lying on the share path older than 7 days. Any suggestion on  this?


thanks in advance
Chandra PAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Fabrice LambertFabrice LambertCommented:
Hi,

Take a look at the FileSystemObject and Folder object:
https://msdn.microsoft.com/en-us/library/hww8txat%28v=vs.84%29.aspx
0
Bill PrewCommented:
What do you really want to do?  By that I meant, what do you mean by a folder "older than 7 days"?  Does that mean a folder that contains no files newer than 7 days?  Or something else?  And how would subfolders factor into this, when a folder has both files and folders underneath it, what is the logic/rules to be followed there?


»bp
0
Chandra PAuthor Commented:
I receive a folder contains 4 files everyday. So in the archive directory(shared) i have multiple folders contains 4 files each. Now i need to delete the folders that are older than 7 days.
0
Python 3 Fundamentals

This course will teach participants about installing and configuring Python, syntax, importing, statements, types, strings, booleans, files, lists, tuples, comprehensions, functions, and classes.

Bill PrewCommented:
Now i need to delete the folders that are older than 7 days.

Okay, and how exactly do you want to determine which folders can be deleted?

  • If all the files are older than 7 days?
  • If the folders Windows create date is older than 7 days?
  • Is the date of the folder part of the folder name that should be looked at?
  • Something else?


»bp
0
Chandra PAuthor Commented:
yes.. based on the each folder's modified date and no need to consider the folder name.

I did tried with objSubFolder.Delete True but nothing is getting deleted.
0
Bill PrewCommented:
Okay, here is a VBS script that should do what you have described.  It looks at all folders in the defined base folder, and if they were modified more than 7 days ago then it will remove them.

strBaseDir = "B:\EE\TEMP"
intKeepDays = 7

Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFolder = objFSO.GetFolder(strBaseDir)

For Each objSubFolder In objFolder.SubFolders
    intAge = DateDiff("d", objSubFolder.DateLastModified, Now)
    If intAge > intKeepDays Then
        Wscript.Echo "DELETE Folder:[" & objSubFolder.Path & "], Age:[" & intAge & "] days"
        objSubFolder.Delete
    End If
Next

Open in new window


»bp
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Chandra PAuthor Commented:
Wooowwww!!!! Works like a magic!!! Thank you mate!!
0
Bill PrewCommented:
Welcome, glad that was useful.


»bp
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
VB Script

From novice to tech pro — start learning today.