VBS to automatically delete FOLDERS

I am trying to create a .vbs that deletes FOLDERS. I do not want to use 3rd-party deletion software such as DELENDA or DELOLD. I also do not want to use Powershell. I have searched for this topic on EE but all the scripts concentrate on files within folders. I only need to delete the folders.

I will be executing the script using a scheduled task.

Here is an example of the folder paths (notice there are spaces in the folder name):

d:\BakupDest\Fri 02.01.2008, 12:30
d:\BakupDest\Sun 02.03.2008, 12:30
d:\BakupDest\Mon 02.04.2008, 12:30

I would like a script that deletes the subfolders of d:\BakupDest\ if they are older than thirty days, by last modified date.

The .vbs below works for files (using last created date) but not for FOLDERS. I have tried changing the methods to Folders instead of files or file but I keep getting script errors. I would like the vbs to perform the same function as the script below but for FOLDERS and modified date of the folder.

'-------------- FILEDELETE.vbs --------------------
Option Explicit
Dim fso
Set fso = CreateObject("Scripting.FileSystemObject")
DeleteFiles fso.GetFolder("c:\PathofMyFolder\")
Sub DeleteFiles(srcFolder)
Dim srcFile
If srcFolder.Files.Count = 0 Then
Exit Sub
End If
For Each srcFile in srcFolder.Files
If DateDiff("d", Now, srcFile.DateCreated) < -30 Then
fso.DeleteFile srcFile, True
End If
End Sub

Thank you for your time.
Who is Participating?
Jeremy_in_JapanConnect With a Mentor Author Commented:
Thanks for your comment BSonPosh.

I have tested and successfully used the Powershell version that you listed. Unfortunately, my organization doesn't want to install and use it on their servers - at this time anyway.

For those of you who aren't restricted, Powershell  is soooo much easier to use! Listen to BsonPosh! :)
Try using the remove directory command (rmdir). Example:

RmDir "C:\bakupdest\sun\"
For others that may be reading this post down the line. Look away "Jeremy_in_Japan"

The Powershell version

get-childitem C:\bakupdest | ?{$_.PSIscontainer -and ($_.LastWriteTime -lt (Get-date).addmonths(-1))} | remove-item -whatif
Jeremy, I understand completely.

Do you know Powershell works perfectly fine using remote filesystem? This means you can use UNC from your machine to accomplish this task.

get-childitem \\server\C$\bakupdest | ?{$_.PSIscontainer -and ($_.LastWriteTime -lt (Get-date).addmonths(-1))} | remove-item -whatif

Anyway... GL with the vbscript version and work on those Powershell hold outs :)
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.

All Courses

From novice to tech pro — start learning today.