Delete files

vmjan02

Well-known Member
Joined
Aug 15, 2012
Messages
1,059
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: 4

Excel Facts

Is there a shortcut key for strikethrough?
Ctrl+S is used for Save. Ctrl+5 is used for Strikethrough. Why Ctrl+5? When you use hashmarks to count |||| is 4, strike through to mean 5.
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,215,092
Messages
6,123,064
Members
449,090
Latest member
fragment

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