Solved Delete Files and Folders, Apart from .zip (1 Viewer)

MattBaldry

Self Taught, Learn from the Forums
Local time
Today, 09:29
Joined
Feb 5, 2019
Messages
293
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
 

Gasman

Enthusiastic Amateur
Local time
Today, 09:29
Joined
Sep 21, 2011
Messages
14,301
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?
 

ebs17

Well-known member
Local time
Today, 10:29
Joined
Feb 7, 2020
Messages
1,946
powershell delete files with exceptions

These would be some useful keywords for your own research.
 

Gasman

Enthusiastic Amateur
Local time
Today, 09:29
Joined
Sep 21, 2011
Messages
14,301
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
 

moke123

AWF VIP
Local time
Today, 04:29
Joined
Jan 11, 2013
Messages
3,920
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
 

MattBaldry

Self Taught, Learn from the Forums
Local time
Today, 09:29
Joined
Feb 5, 2019
Messages
293
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
 

MattBaldry

Self Taught, Learn from the Forums
Local time
Today, 09:29
Joined
Feb 5, 2019
Messages
293
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
 

Gasman

Enthusiastic Amateur
Local time
Today, 09:29
Joined
Sep 21, 2011
Messages
14,301
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. :(
 

moke123

AWF VIP
Local time
Today, 04:29
Joined
Jan 11, 2013
Messages
3,920
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:

MattBaldry

Self Taught, Learn from the Forums
Local time
Today, 09:29
Joined
Feb 5, 2019
Messages
293
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
 

Gasman

Enthusiastic Amateur
Local time
Today, 09:29
Joined
Sep 21, 2011
Messages
14,301
Well as you have been given code that just selects certain files, I would go with that method?
 

MattBaldry

Self Taught, Learn from the Forums
Local time
Today, 09:29
Joined
Feb 5, 2019
Messages
293
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
 

Gasman

Enthusiastic Amateur
Local time
Today, 09:29
Joined
Sep 21, 2011
Messages
14,301
Oops, missed the folder part. :(
 

moke123

AWF VIP
Local time
Today, 04:29
Joined
Jan 11, 2013
Messages
3,920
Are there files within the folders your deleting? Do you need the code to be recursive?
 

MattBaldry

Self Taught, Learn from the Forums
Local time
Today, 09:29
Joined
Feb 5, 2019
Messages
293
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

Top Bottom