Delete in subfolders except one file.

KlausW

Active Member
Joined
Sep 9, 2020
Messages
478
Office Version
  1. 2016
Platform
  1. Windows
Hi everyone

I am using this VBA code to delete PNG files in a subfolder.
It won't even though the subfolder address is in I3. And look like this D:\DNBR 2025\Rejseafregning\Bilag
Can anyone help.

Any help would be appreciated

Best regards Klaus W

VBA Code:
Sub Del()

    Dim path As String
    path = Range("I3")
    If Right(path, 1) <> "\" Then
        path = path & "\"
    End If
    
    Dim exceptedFile As String
    exceptedFile = "B1.png"
    
    Dim currentFile As String
    currentFile = Dir(path & "*.*", vbNormal)
    
    While (Len(currentFile) > 0)
        If LCase(currentFile) <> LCase(exceptedFile) Then
            Kill path & currentFile
        End If
        currentFile = Dir
    Wend
    
    MsgBox "Bilag slettet!", vbExclamation

 
 
   
   End Sub
 
I found the solution myself. Here at Mr. Excel

 
Upvote 0
Solution
Hi KlausW, you don't need Excel for that. You can easily accomplish the same thing using a DOS batch file. Just place the shortcut for it on your desktop and run it whenever you need it.
 
Upvote 0

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top