Delete files

vmjan02

Well-known Member
Joined
Aug 15, 2012
Messages
1,134
Office Version
  1. 365
  2. 2021
  3. 2019
  4. 2016
  5. 2013
I have this script and is all perfect for keepFile1 and keepfile2, but not shre as why it is not working for keepFile3

it is deleting all the unwanted files but for keepFile3 is not norking as desired, any modifications. screen shot attached for the folder.

VBA Code:
Sub RemoveUnwantedFiles()
    Dim fso As Object
    Dim folderPath As String
    Dim file As Object
    Dim fileName As String
    Dim keepFile1 As String
    Dim keepFile2 As String
    Dim keepFile3 As String
    
    ' Set the folder path
    folderPath = ActiveWorkbook.Path & "\" & "RawData NE" & "\" & "Fiber"
    
    ' Set the filenames to keep
    keepFile1 = "NE_Report_"
    keepFile2 = "QueryDumpNOKIA5520AMS_FIBER"
    keepFile3 = "QueryDumpNOKIA5520AMS_FIBER-8"   
    
    ' Create FileSystemObject
    Set fso = CreateObject("Scripting.FileSystemObject")
    
    ' Check if the folder exists
    If fso.FolderExists(folderPath) Then
        ' Loop through each file in the folder
        For Each file In fso.GetFolder(folderPath).Files
            fileName = fso.GetFileName(file)
            
            ' Check if the file name matches without considering the date part from the left and delete it if it doesn't match
            If Not (fileName Like keepFile1 & "*" Or _
                    fileName Like keepFile2 & "*" Or _
                    (fileName Like keepFile3 & "*" And InStr(fileName, "-8_") > 0)) Then   ''' this part is not working.
                fso.DeleteFile file.Path
            End If
        Next file
    Else
        MsgBox "Folder does not exist: " & folderPath, vbExclamation
    End If
    
    ' Release the FileSystemObject
    Set fso = Nothing
End Sub



[ATTACH type="full"]104779[/ATTACH]
 

Attachments

  • 1704957799490.png
    1704957799490.png
    75.3 KB · Views: 9

Excel Facts

How to change case of text in Excel?
Use =UPPER() for upper case, =LOWER() for lower case, and =PROPER() for proper case. PROPER won't capitalize second c in Mccartney
how about (other position of parenthesis)

VBA Code:
(fileName Like keepFile1 & "*" Or fileName Like keepFile2 & "*" Or fileName Like keepFile3 & "*") And InStr(fileName, "-8_") > 0 Then
 
Upvote 0
Try:

VBA Code:
If Not ((fileName Like keepFile1 & "*" Or fileName Like keepFile2 & "*") Or (fileName Like keepFile3 & "*" And InStr(fileName, "-8_") > 0)) Then
 
Upvote 0
Try:

VBA Code:
If Not ((fileName Like keepFile1 & "*" Or fileName Like keepFile2 & "*") Or (fileName Like keepFile3 & "*" And InStr(fileName, "-8_") > 0)) Then

still the same.
keepFile3 is the concern.

1704964697840.png
 
Upvote 0

Forum statistics

Threads
1,224,814
Messages
6,181,123
Members
453,021
Latest member
Justyna P

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