PDA

View Full Version : macro execuation to delete folders



dpatel
05-22-2013, 08:36 AM
Hello,

I have number of folders by months "ddmmyyyy" format.

So i would like to remove last 2 months folders (from now() to last 2 months).

Can someone please help me here?

Thanks,

mancubus
05-23-2013, 04:21 AM
hi.
try this.
test with empty folders first :)
i assume all subfolder names are 8 chars long...

today (for ex 23052013) and same day in 2 months before today (for ex 23032013) are included with >= and <= operators. change to suit your case.


Sub Delete_SubFolders_AccTo_NameAsDate()

Dim fso, fFolder, fSubFolders, sFld, SFDate, fPath

fPath = "C:\Test" 'parent folder. change to suit

Set fso = CreateObject("Scripting.FileSystemObject")
Set fFolder = fso.GetFolder(fPath)
Set fSubFolders = fFolder.SubFolders

For Each sFld In fSubFolders
sYear = Mid(sFld.Name, 5)
sMonth = Mid(sFld.Name, 3, 2)
sDay = Left(sFld.Name, 2)
SFDate = DateSerial(Val(sYear), Val(sMonth), Val(sDay))

If (SFDate >= DateAdd("m", -2, Date) And SFDate <= Date) Then
fso.deletefolder sFld
End If
Next
End Sub