Kill files from excel (VBA)

Dear Experts:

Below code deletes all png files located in the folder "C:\Test\"

Could somebody please rewrite the code that the following requirements are met:

- nested subfolders under C:\Test\ should also be worked on
- all png-files should be deleted with the exception of file names with the following make-up: ##-###-##-##.png (e.g. 90-434-22-43.png or 55-234-55-07.png)
- will the below and revised code run on 64 bit windows Systems as well?

Help is much appreciated. Thank you very much in advance.

Regards, Andreas

Sub DeletePNGs()
Dim fs As FileSystemObject
Dim f As Object
Dim strFileName As String
Const strPath As String = "C:\Test\" 'The folder with the files
Set fs = CreateObject("Scripting.FileSystemObject")
strFileName = Dir$(strPath & "*.png")

While Len(strFileName) <> 0
    Set f = fs.GetFile(strPath & strFileName)
    Kill strPath & strFileName
    strFileName = Dir$()
Wend

Set f = Nothing
Set fs = Nothing
End Sub

Open in new window

Andreas HermleTeam leaderAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

aikimarkCommented:
This should come close
Sub DeletePNGs()
    Dim fs As Object
    Dim f As Object
    Const strPath As String = "C:\test\" 'The folder with the files
    Set fs = CreateObject("Scripting.FileSystemObject")
    strFileName = Dir$(strPath & "*.png")
    For Each f In fs.GetFolder(strPath).Files
        If LCase(f.Name) Like "*.png" And Not (LCase(f.Name) Like "##-###-##-##.png") Then
            fs.Deletefile strPath & f.Name
        End If
    Next

    Set f = Nothing
    Set fs = Nothing

End Sub

Open in new window

0
Bill PrewCommented:
@aikimark

I think the OP also wanted to recursively drill into all subfolders.

~bp
0
aikimarkCommented:
@bp

Right you are.  Missed that requirement.  It's getting late and my eyes are starting to fuzz.  If the question is still open in the morning, I'll post an updated version of the code that will traverse the folders.
0
Cloud Class® Course: Microsoft Office 2010

This course will introduce you to the interfaces and features of Microsoft Office 2010 Word, Excel, PowerPoint, Outlook, and Access. You will learn about the features that are shared between all products in the Office suite, as well as the new features that are product specific.

Andreas HermleTeam leaderAuthor Commented:
Dear both,

thank you very much for your swift support.

Yes, as a matter of fact, I would like to have subfolders also worked on.

In the meantime, I will try your version on one folder.

Thank you

Regards, Andreas
0
aikimarkCommented:
Here is a tweaked version of the code.  You should invoke the Q_28242802() routine that will start the recursive iteration of files and sub-directories.
Option Explicit

Public Sub Q_28242802()
    Const strPath As String = "C:\test" 'Top level folder with the files
    DeletePNGs strPath
End Sub

Public Sub DeletePNGs(parmPath)
    Dim fs As Object
    Dim oFile_or_Dir As Object
    Set fs = CreateObject("Scripting.FileSystemObject")
    For Each oFile_or_Dir In fs.GetFolder(parmPath).Files
        If LCase(oFile_or_Dir.Name) Like "*.png" Then
            If (LCase(oFile_or_Dir.Name) Like "##-###-##-##.png") Then
            Else
                fs.Deletefile parmPath & "\" & oFile_or_Dir.Name
            End If
        End If
    Next
    For Each oFile_or_Dir In fs.GetFolder(parmPath).SubFolders
        DeletePNGs oFile_or_Dir.Path
    Next
    Set oFile_or_Dir = Nothing
    Set fs = Nothing

End Sub

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Andreas HermleTeam leaderAuthor Commented:
Great job. Thank you very much for your great support.

The code first threw an error message: Permission denied. But I changed permission properties on the folder in question and everything went according to plan.

Again, thank you very  much for your great and professional help.

When I get back to my workplace next week, I will test this macro on my 64 bit Windows System. Hopefully, it will work there, too.

regards, Andreas
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft Excel

From novice to tech pro — start learning today.