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.
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]