Solved Delete Files and Folders, Apart from .zip

MattBaldry

Self Taught, Learn from the Forums
Local time
Today, 01:59
Joined
Feb 5, 2019
Messages
325
Hi all,

I have looked but cannot find a method for this. I want to delete all files and folders from a main folder, apart from anything *.zip.

I can find code to delete everything, or all of a certain file type, but nothing to only leave a certain file type.

~Matt
 
Check each file for the extension.?
Might be easier to copy zip to a new temp folder, delete all files, copy back and remove the temp folder?
 
powershell delete files with exceptions

These would be some useful keywords for your own research.
 
I used jpg as an example
I was interested to see how it would work, when a file was deleted, but it seems to move on to the next file.

I would however test it thoroughly on a copy of your folder.
Code:
Sub RemoveNonZip()
Dim strFile As String, strDir As String

strDir = "F:\Temp\"
strFile = Dir(strDir & "*.*")
Do While strFile <> ""
    Debug.Print strFile
    If Right(strFile, 4) = ".jpg" Then
        Kill strDir & strFile
    End If
    strFile = Dir
Loop

End Sub
 
Code:
Dim fso As New FileSystemObject

    Dim fol As Folder, fil As File

    Set fol = fso.GetFolder("The path to your folder")

    For Each fil In fol.Files

        If Not fso.GetExtensionName(fil.Path) = "zip" Then
            Debug.Print fil.Name
            'fso.DeleteFile fil.Path
        End If

    Next

    set fso = nothing
 
I used jpg as an example
I was interested to see how it would work, when a file was deleted, but it seems to move on to the next file.

I would however test it thoroughly on a copy of your folder.
Code:
Sub RemoveNonZip()
Dim strFile As String, strDir As String

strDir = "F:\Temp\"
strFile = Dir(strDir & "*.*")
Do While strFile <> ""
    Debug.Print strFile
    If Right(strFile, 4) = ".jpg" Then
        Kill strDir & strFile
    End If
    strFile = Dir
Loop

End Sub
Hi Gasman,

I cannot seem to get this to work. It doesn't remove anything :(

~Matt
 
Code:
Dim fso As New FileSystemObject

    Dim fol As Folder, fil As File

    Set fol = fso.GetFolder("The path to your folder")

    For Each fil In fol.Files

        If Not fso.GetExtensionName(fil.Path) = "zip" Then
            Debug.Print fil.Name
            'fso.DeleteFile fil.Path
        End If

    Next

    set fso = nothing
Hi moke123,

This just gives an error as below.

1711018296988.png


~Matt
 
Hi Gasman,

I cannot seem to get this to work. It doesn't remove anything :(

~Matt
Use Moke's then, just another method.
Note I was only giving you the logic, YOU have to adapt for your situation.

If you were using AS IS it would only delete jpg files. :(
 
That was early bound so you need to set a reference to Microsoft Scripting runtime.

This is the same code late bound so no reference needed.

Code:
Dim fso As Object
    Set fso = CreateObject("Scripting.FileSystemObject")

    Dim fol As Object, fil As Object

    Set fol = fso.GetFolder(" Your folder path ")

    For Each fil In fol.Files

        If Not fso.GetExtensionName(fil.Path) = "zip" Then
            Debug.Print fil.Name
            'fso.DeleteFile fil.Path
        End If

    Next

    Set fso = Nothing
 
Last edited:
Check each file for the extension.?
Might be easier to copy zip to a new temp folder, delete all files, copy back and remove the temp folder?
Hi Gasman,

I went with this approach in the end. I create the zip file in a temp folder, clear the first folder, then move the file from Temp to Original.

I will update this thread once I have it working. At the moment it is deleting the files/folders before the zip has complete.

~Matt
 
Well as you have been given code that just selects certain files, I would go with that method?
 
Well as you have been given code that just selects certain files, I would go with that method?
Hi Gasman,

That code only deleted files and not folders. Now create the zip file in a temp location, empty the source folder then move the zip file back.

I had to put a 2 second pause after the zip file is created otherwise it deleted the files before it was finished. I used the code on the link below to clear files and folders.


It now works as intended and all is good in the world.

~Matt
 
Are there files within the folders your deleting? Do you need the code to be recursive?
 
Are there files within the folders your deleting? Do you need the code to be recursive?
There are files in the folder and subfolder. The code I have now works fine which is good. I create the zip in a temp folder. Clear the main folder then move the zip to the main folder from the temp one. It's not elegant, but works for the basic needs we have. Thank you for your help, your code helped me find the one I used in the end.

~Matt
 

Users who are viewing this thread

Back
Top Bottom