[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now


Need a script

Posted on 2011-09-21
Medium Priority
Last Modified: 2012-05-12

I need a script to identify what are the folders that are 7 days old in a particular path and to delete those folders automatically. The script should look only for folders and not for files. Thanks
Question by:jmohan0302
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
  • 4
  • 3
  • 2

Expert Comment

ID: 36576377
Have you looked at the Forfiles command, it can find files or folders of a certain age:

This example, removes directories older than 20 days in c:\temp.

 forfiles /p "c:\temp" /c "cmd /c if @isdir==TRUE RMDIR /S /Q @path" /d -20

Expert Comment

ID: 36576385
... sorry only saw now you asking about vb

Author Comment

ID: 36577971
Not a problem I need any thing that fulfills my work. Even a command or a script any thing
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

LVL 57

Expert Comment

by:Bill Prew
ID: 36578011
This should be close, adjust the constants at the top as needed.  Right now the actual delete is commented out, and it will just display the folders it thinks should be deleted.  This is for testing, if it works right uncomment out the delete.  Also, it is checking the date the folder was created currently, not last modified, since you didn't specify I wasn't sure so started with this.

Const conBaseDir = "C:\EE\Temp"
Const conDelAge = 7

' Create file system object
Set objFSO = WScript.CreateObject("Scripting.FileSystemObject")

' Call recursive routine to clean out old folders
CleanFolder objFSO.GetFolder(conBaseDir), conDelAge

' Release file system object
Set objFSO = Nothing

' Recursive subroutine to remove old subfolders
Sub CleanFolder(objFolder, intAge)

   ' See if this folders subfolders are old enough to delete.
   For Each objSubFolder In objFolder.SubFolders
      If DateDiff("d", objSubFolder.DateCreated, Now) >= intAge Then
         ' objSubFolder.Delete
         Wscript.Echo "Deleted Folder: [" & objSubFolder.Path & "], Age: [" & DateDiff("d", objSubFolder.DateCreated, Now) & "]"
         CleanFolder objSubFolder, intAge
      End If

End Sub

Open in new window


Author Comment

ID: 36578196
Hi Billprew, Thanks for your reply. Yet I didnt check. I will check and let you know the output. I just want to know that it has to delete all the folders that is 7 days old. Thanks again

Author Comment

ID: 36582303
Hi Gavincr001,

Please let me know from where you got these kind of commands. I just want to create these kinds of commands on own.  Any help from your end will be greatfull. Thanks
LVL 57

Expert Comment

by:Bill Prew
ID: 36584254
ForFiles is an add on utility for Windows from Microsoft.  It was released in some of the Resource Kits and now is also part of Windows 7.  Further info can be found at:




Author Comment

ID: 36690001
Hi All,

The below forfiles command is working fine in Win7,WIn2003 and WIn2008:

forfiles /p "e:\shilpa" /c "cmd /c if @isdir==TRUE RMDIR /S /Q @path" /d -20

But it is not working in win2000.

Need help to use the forfiles command in 2000 to delete the folders in a path which is 7 days old.


Accepted Solution

Gavincr001 earned 2000 total points
ID: 36712966
You can try copy the forfiles.exe from Win7 or server 2008 to your Win2000 PC.

Or setup the scheduled task from a Win7 PC to delete the files on a mapped drive/UNC path to the Win2000 server.


Featured Post

[Webinar] Lessons on Recovering from Petya

Skyport is working hard to help customers recover from recent attacks, like the Petya worm. This work has brought to light some important lessons. New malware attacks like this can take down your entire environment. Learn from others mistakes on how to prevent Petya like worms.

Question has a verified solution.

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

Hello again, all.  For those of you that have been following along, you'll know that this is my third article on this topic (though it is not Part III).  This article is sort of remedial, and probably the topic with which I should have started the s…
Not long ago I saw a question in the VB Script forum that I thought would not take much time. You can read that question (Question ID  (http://www.experts-exchange.com/Programming/Languages/Visual_Basic/VB_Script/Q_28455246.html)28455246) Here (http…
This course is ideal for IT System Administrators working with VMware vSphere and its associated products in their company infrastructure. This course teaches you how to install and maintain this virtualization technology to store data, prevent vuln…
Is your data getting by on basic protection measures? In today’s climate of debilitating malware and ransomware—like WannaCry—that may not be enough. You need to establish more than basics, like a recovery plan that protects both data and endpoints.…
Suggested Courses

656 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