+ Reply to Thread
Results 1 to 6 of 6

Thread: Kill all non excel files in folder

  1. #1
    Registered User
    Join Date
    04-06-2009
    Location
    England
    MS-Off Ver
    Excel 2003
    Posts
    92

    Kill all non excel files in folder

    hi there

    i was wondering how to kill all files in this folder:
    H:\Emails

    that are NOT excel files, any ideas?

    Thanks

  2. #2
    Valued Forum Contributor Kyle123's Avatar
    Join Date
    03-10-2010
    Location
    Leeds/Sheffield, England
    MS-Off Ver
    Excel 2003
    Posts
    1,031

    Re: Kill all non excel files in folder

    Something like:

    Sub killfiles(fPath As String)
    Dim file As Object
    Const ext As String = "xls*"
    
    With CreateObject("scripting.filesystemobject")
        For Each file In .getfolder(fPath).Files
          If Not .getextensionname(file) Like ext Then
                Debug.Print file.Name
                'file.Delete
         End If
        Next
    End With
    
    End Sub
    You'll need to uncomment the delete part, use it in its current form to see what if would delete
    Click the * below to say thanks

    Girls sleep with guys who use photoshop, but marry the ones who work with Excel

    Corduroy
    pillows: They're making headlines!

    Did you mean: recursion
    http://www.google.com/search?hl=en&q=recursion

  3. #3
    Forum Guru
    Join Date
    03-12-2010
    Location
    Canada
    MS-Off Ver
    Excel 2010
    Posts
    1,974

    Re: Kill all non excel files in folder

    This is something I put together a while back. It also checks subfolders.

    Option Explicit
    
    Sub DeleteFiles()
    
        Dim objFSO
        Dim strPath As String
    
        With Application.FileDialog(msoFileDialogFolderPicker)
            .Title = "Please Select a Folder"
            .ButtonName = "Select Folder"
            .InitialFileName = "E:\Emails\"
            .AllowMultiSelect = False
            .Show
            If .SelectedItems.Count > 0 Then
                strPath = .SelectedItems(1) & "\"
            Else
                MsgBox "No folder was chosen." & vbLf & vbLf & "Please try again.", vbExclamation, "User Cancelled."
                Exit Sub
            End If
        End With
        
        With Application
            .Calculation = xlCalculationManual
            .ScreenUpdating = False
            .EnableEvents = False
            .DisplayAlerts = False
        End With
        
        Set objFSO = CreateObject("Scripting.FileSystemObject")
        
        Call ProcessFolders(objFSO, strPath)
        
        With Application
            .ScreenUpdating = True
            .EnableEvents = True
            .DisplayAlerts = True
        End With
        
        MsgBox "Completed...", vbInformation
        
    End Sub
    
    Sub ProcessFolders(ByRef fso, ByVal fpath)
    
        Dim objFolder
        Dim objSubFolder
        Dim objFile
        Dim wkb As Workbook
        
    On Error Resume Next
        Set objFolder = fso.GetFolder(fpath)
        
        For Each objFile In objFolder.Files
            If LCase(objFile.Name) Like "*.xl*" Then Next objFile Else Kill (objFile)
        Next objFile
        
        For Each objSubFolder In objFolder.SubFolders
            Call ProcessFolders(fso, objSubFolder)
        Next objSubFolder
    
    End Sub
    Remeber to test it on a non-essential folder first because you can't undelet the files.

    abousetta
    Please consider:

    Thanking those who helped you. Click the star icon in the lower left part of the contributor's post and add Reputation.
    Cleaning up when you're done. Mark your thread [SOLVED] if you received your answer.

  4. #4
    Forum Guru
    Join Date
    03-12-2010
    Location
    Canada
    MS-Off Ver
    Excel 2010
    Posts
    1,974

    Re: Kill all non excel files in folder

    Hi Kyle,

    I would change *.xls* to *.xl* in your code to catch some of the older versions of Excel files like *.xlt.

    abousetta
    Please consider:

    Thanking those who helped you. Click the star icon in the lower left part of the contributor's post and add Reputation.
    Cleaning up when you're done. Mark your thread [SOLVED] if you received your answer.

  5. #5
    Valued Forum Contributor Kyle123's Avatar
    Join Date
    03-10-2010
    Location
    Leeds/Sheffield, England
    MS-Off Ver
    Excel 2003
    Posts
    1,031

    Re: Kill all non excel files in folder

    Yep, do as abousetta suggested missed that one

    @abousetta, I'm curious as to why you turned off all the Excel updating, events etc... As far as I can see in your code, this would have little effect as you are not doing anything that would trigger any of that. Have I missed something?
    Click the * below to say thanks

    Girls sleep with guys who use photoshop, but marry the ones who work with Excel

    Corduroy
    pillows: They're making headlines!

    Did you mean: recursion
    http://www.google.com/search?hl=en&q=recursion

  6. #6
    Forum Guru
    Join Date
    03-12-2010
    Location
    Canada
    MS-Off Ver
    Excel 2010
    Posts
    1,974

    Re: Kill all non excel files in folder

    Yes, you missed the obvious... my bad habits

    I just got used to putting this around all my code and I got lazy and didn't remove it for this particular macro.

    abousetta
    Please consider:

    Thanking those who helped you. Click the star icon in the lower left part of the contributor's post and add Reputation.
    Cleaning up when you're done. Mark your thread [SOLVED] if you received your answer.

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

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.2.0