Consulting

Results 1 to 2 of 2

Thread: macro execuation to delete folders

  1. #1
    VBAX Regular
    Joined
    Jul 2010
    Posts
    15
    Location

    macro execuation to delete folders

    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,

  2. #2
    VBAX Guru mancubus's Avatar
    Joined
    Dec 2010
    Location
    "Where I lay my head is home" :D
    Posts
    2,644
    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.

    [vba]
    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
    [/vba]
    PLS DO NOT PM; OPEN A THREAD INSTEAD!!!

    1) Posting Code
    [CODE]PasteYourCodeHere[/CODE]
    (or paste your code, select it, click # button)

    2) Uploading File(s)
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) (multiple files can be selected while holding Ctrl key) / Upload Files / Done
    Replace company specific / sensitive / confidential data. Include so many rows and sheets etc in the uploaded workbook to enable the helpers visualize the data and table structure. Helpers do not need the entire workbook.

    3) Testing the Codes
    always back up your files before testing the codes.

    4) Marking the Thread as Solved
    from Thread Tools (on the top right corner, above the first message)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •