• Status: Solved
  • Priority: Low
  • Security: Public
  • Views: 26
  • Last Modified:

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
0
Chandra P
Asked:
Chandra P
  • 4
  • 3
1 Solution
 
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
Cloud Class® Course: CompTIA Cloud+

The CompTIA Cloud+ Basic training course will teach you about cloud concepts and models, data storage, networking, and network infrastructure.

 
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
 
Chandra PAuthor Commented:
Wooowwww!!!! Works like a magic!!! Thank you mate!!
0
 
Bill PrewCommented:
Welcome, glad that was useful.


»bp
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: 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.

  • 4
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now