+ Reply to Thread
Results 1 to 7 of 7

Delete old files in a specific folder

Hybrid View

forfiett Delete old files in a... 08-26-2014, 02:52 PM
mehmetcik Re: Delete old files in a... 08-26-2014, 05:42 PM
forfiett Re: Delete old files in a... 08-27-2014, 01:14 AM
HaHoBe Re: Delete old files in a... 08-27-2014, 01:22 AM
forfiett Re: Delete old files in a... 08-27-2014, 01:26 AM
HaHoBe Re: Delete old files in a... 08-27-2014, 12:15 PM
forfiett Re: Delete old files in a... 09-26-2014, 02:37 AM
  1. #1
    Forum Contributor
    Join Date
    12-13-2012
    Location
    Italy
    MS-Off Ver
    Excel 2010
    Posts
    162

    Delete old files in a specific folder

    Good evening,

    While searching on google for a code that will delte all files that are old more than xx days in a specific folder, I found out bellow code:

    Sub DelOldFiles() 
         
         
         ''Clear out all files over 7 days old from Dir_Path folder.
         '
        Dir_Path = "C:\Folder\SubFolder\" 
        iMaxAge = 7 ' Set the number of days
         
        Set oFSO = CreateObject("Scripting.FileSystemObject") 
        If oFSO.FolderExists(Dir_Path) Then 'Check that the folder exists
            For Each oFile In oFSO.GetFolder(Dir_Path).Files 
                If DateDiff("d", oFile.DateLastModified, Now) > iMaxAge Then 'Look at each file to check if it is older than 7 days
                    oFile.Delete 
                End If 
            Next 
        End If 
    
        End Sub
    but unforunatelly is not working for me.
    It pups up a error saying: Dir_Path = "C:\Folder\SubFolder\" --> VARIABLE NOT DEFINED.

    Do you have any Idea how to handle it?

    Best regards,
    forfiett
    I get a error

  2. #2
    Forum Expert
    Join Date
    12-14-2012
    Location
    London England
    MS-Off Ver
    MS 365 Office Suite.
    Posts
    8,448

    Re: Delete old files in a specific folder

    The macro is looking for the folder called "Subfolder" in the Folder "Folder" in your C Drive.

    Does it exist?

  3. #3
    Forum Contributor
    Join Date
    12-13-2012
    Location
    Italy
    MS-Off Ver
    Excel 2010
    Posts
    162

    Re: Delete old files in a specific folder

    Good day,

    Yes it exist.

    Best regards,
    forfiett

  4. #4
    Forum Guru HaHoBe's Avatar
    Join Date
    02-19-2005
    Location
    Hamburg, Germany
    MS-Off Ver
    work: 2016 on Win10 (notebook), private: 365 on Win11 (desktop), 2019 on Win11 (notebook)
    Posts
    8,198

    Re: Delete old files in a specific folder

    Hi, forfiett,

    what about using
        Dir_Path = "C:\Folder\SubFolder"
    without the last backslash?

    Ciao,
    Holger
    Use Code-Tags for showing your code: [code] Your Code here [/code]
    Please mark your question Solved if there has been offered a solution that works fine for you

  5. #5
    Forum Contributor
    Join Date
    12-13-2012
    Location
    Italy
    MS-Off Ver
    Excel 2010
    Posts
    162

    Re: Delete old files in a specific folder

    Dear Holger,

    Have tried also this option without result.
    Maybe do you have any other similar code?

    Best regards,
    forfiett

  6. #6
    Forum Guru HaHoBe's Avatar
    Join Date
    02-19-2005
    Location
    Hamburg, Germany
    MS-Off Ver
    work: 2016 on Win10 (notebook), private: 365 on Win11 (desktop), 2019 on Win11 (notebook)
    Posts
    8,198

    Re: Delete old files in a specific folder

    Hi, forfiett,

    if you have fuill access to the folder and the files are of file extension xls maybe try it like this:
    Sub EF1033426()
    
    Dim fso As Object
    Dim fso_Folder As Object
    Dim fso_FILE As Object
    Dim strFilePath As String
    Dim strFileExt As String
    
    Const clngMAX_AGE As Long = 7
    
    strFilePath = "C:\Folder\SubFolder"
    strFileExt = "xl"
    
    Set fso = CreateObject("Scripting.FileSystemObject")
    Set fso_Folder = fso.GetFolder(strFilePath)
    
    If fso_Folder.Files.Count > 0 Then
      For Each fso_FILE In fso_Folder.Files
        If fso.GetExtensionName(fso_FILE.Name) Like strFileExt & "*" Then
          If CLng(Date) - CLng(FileDateTime(fso_FILE)) >= clngMAX_AGE Then
            fso_FILE.Delete
          End If
        End If
      Next fso_FILE
    Else
      MsgBox "No Files Found at " & strFilePath
    End If
    
    Set fso_Folder = Nothing
    Set fso = Nothing
    
    End Sub
    Please mind that all files of the extension xl* should be deleted in the givern directory if the are older than seven days.

    Ciao,
    Holger

  7. #7
    Forum Contributor
    Join Date
    12-13-2012
    Location
    Italy
    MS-Off Ver
    Excel 2010
    Posts
    162

    Re: Delete old files in a specific folder

    Good day,

    I will try it. Thanks.

    Best regards,
    forfiett

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Replies: 1
    Last Post: 01-28-2019, 06:55 PM
  2. Extract specific column data from specific sheet from multiple files in a folder
    By piggyfox in forum Excel Programming / VBA / Macros
    Replies: 10
    Last Post: 05-31-2013, 11:51 AM
  3. Replies: 1
    Last Post: 01-24-2006, 11:35 AM
  4. Replies: 1
    Last Post: 01-24-2006, 11:00 AM
  5. Replies: 1
    Last Post: 01-24-2006, 12:25 AM

Bookmarks

Posting Permissions

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

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1