Link to home
Start Free TrialLog in
Avatar of Jeremy_in_Japan
Jeremy_in_Japan

asked on

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
Next
End Sub
'-----------------------------------------------

Thank you for your time.
Avatar of dinomix
dinomix

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
ASKER CERTIFIED SOLUTION
Avatar of Jeremy_in_Japan
Jeremy_in_Japan

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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 :)