Check file name in foder and remove similar

nhnn1986

Board Regular
Joined
Oct 12, 2017
Messages
92
Hi all:

I want macro to check all file name in folder, if file name similar (different one or two charactor) will create new folder "OLD" then remove file with number smaller to folder "OLD"

Example: Folder have:
G03144-99201001-01201001-201906-BI-M-01.xlsx
G03144-99201001-01201001-201906-BI-M-02.xlsx
G03144-99201001-01201001-201906-BI-M-05.xlsx
G03144-99201001-01201001-201906-BI-M-10.xlsx
G03144-99201001-01201001-201906-BI-M-15.xlsx

=> Create Folder "OLD" and remove
G03144-99201001-01201001-201906-BI-M-01.xlsx, G03144-99201001-01201001-201906-BI-M-02.xlsx, G03144-99201001-01201001-201906-BI-M-05.xlsx, G03144-99201001-01201001-201906-BI-M-10.xlsx

Folder only have:
G03144-99201001-01201001-201906-BI-M-15.xlsx

Thanks./.
 

Excel Facts

Waterfall charts in Excel?
Office 365 customers have access to Waterfall charts since late 2016. They were added to Excel 2019.
Re: Help to check file name in foder and remove similar

Hi, i have a couple of doubts:
Are they always the last 2 digits of the file name?
Are there more files in the folder, or only 5 files?
 
Upvote 0
Re: Help to check file name in foder and remove similar

Hi, i have a couple of doubts:
Are they always the last 2 digits of the file name?
Are there more files in the folder, or only 5 files?

Hi Dante:
yes, there're always have 02 digital at last and folder have more files, over 900 files

File always look like:
G03144-xxxxxxxx-yyyyyyyy-201906-BI-M-01.xlsx
A00024-xxxxxxxx-yyyyyyyy-201906-BI-M-05.xlsx
F00004-xxxxxxxx-yyyyyyyy-201906-BI-M-15.xlsx
.... and more
 
Last edited:
Upvote 0
Re: Help to check file name in foder and remove similar

Hi @nhnn1986, Try this

Change the red letters in the macro for the name of your folder

Code:
Option Explicit


Sub check_file_name()
'check all file name in folder then remove file with number smaller to folder "OLD"
  Dim folder As String, folderOld As String, wName As String, wData As String, wArch As String
  Dim dict As Object, FSO As Object, n As Long
  Dim wFiles As Variant, wNum As Variant, wMax As Variant
  '
  folder = "[B][COLOR=#ff0000]C:\trabajo\books\[/COLOR][/B]"
  If Right(folder, 1) <> "\" Then folder = folder & "\"
  If Dir(folder, vbDirectory) = "" Then
    MsgBox "Folder does not exist", vbCritical
    Exit Sub
  End If
  folderOld = folder & "OLD\"
  If Dir(folderOld, vbDirectory) = "" Then
    MkDir folderOld
  End If
  '
  Set FSO = CreateObject("Scripting.Filesystemobject")
  Set dict = CreateObject("scripting.dictionary")
  wFiles = Dir(folder)
  Do While wFiles <> ""
    wNum = Val(Mid(wFiles, InStrRev(wFiles, "-") + 1, 2))
    wName = Left(wFiles, InStrRev(wFiles, "-") - 1)
    wData = wNum & "|" & wFiles
    '
    If Not dict.exists(wName) Then
      dict.Add wName, wData
    Else
      wMax = Val(Split(dict(wName), "|")(0))
      wArch = Split(dict(wName), "|")(1)
      If wNum > wMax Then
        FSO.MoveFile Source:=folder & wArch, Destination:=folderOld & wArch
        dict(wName) = wData
      Else
        FSO.MoveFile Source:=folder & wFiles, Destination:=folderOld & wFiles
      End If
      n = n + 1
    End If
    '
    wFiles = Dir()
  Loop
  MsgBox "Files removed: " & n
End Sub
 
Upvote 0
Re: Help to check file name in foder and remove similar

Thanks for your code @Dante, code worrk well.

I add some code to change that: Brown to choose folder like below code:
Code:
Option Explicit



Sub check_file_name()
'check all file name in folder then remove file with number smaller to folder "OLD"
  Dim folder As String, folderOld As String, wName As String, wData As String, wArch As String
  Dim dict As Object, FSO As Object, n As Long
  Dim wFiles As Variant, wNum As Variant, wMax As Variant
[COLOR=#ff0000]With Application.FileDialog(msoFileDialogFolderPicker)[/COLOR]
[COLOR=#ff0000]        .Title = "Select a Folder"[/COLOR]
[COLOR=#ff0000]        If .Show = -1 Then ' if OK is pressed[/COLOR]
[COLOR=#ff0000]            folder = .SelectedItems(1)[/COLOR]
[COLOR=#ff0000]        End If[/COLOR]
[COLOR=#ff0000]End With[/COLOR]
[COLOR=#ff0000]  If folder = "" Then Exit Sub[/COLOR]
  If Right(folder, 1) <> "\" Then folder = folder & "\"
  If Dir(folder, vbDirectory) = "" Then
    MsgBox "Folder does not exist", vbCritical
    Exit Sub
  End If
  folderOld = folder & "OLD\"
  If Dir(folderOld, vbDirectory) = "" Then
    MkDir folderOld
  End If
  '
  Set FSO = CreateObject("Scripting.Filesystemobject")
  Set dict = CreateObject("scripting.dictionary")
  wFiles = Dir(folder)
  Do While wFiles <> ""
    wNum = Val(Mid(wFiles, InStrRev(wFiles, "-") + 1, 2))
    wName = Left(wFiles, InStrRev(wFiles, "-") - 1)
    wData = wNum & "|" & wFiles
    '
    If Not dict.exists(wName) Then
      dict.Add wName, wData
    Else
      wMax = Val(Split(dict(wName), "|")(0))
      wArch = Split(dict(wName), "|")(1)
      If wNum > wMax Then
        FSO.MoveFile Source:=folder & wArch, Destination:=folderOld & wArch
        dict(wName) = wData
      Else
        FSO.MoveFile Source:=folder & wFiles, Destination:=folderOld & wFiles
      End If
      n = n + 1
    End If
    '
    wFiles = Dir()
  Loop
  MsgBox "Files removed: " & n
End Sub
 
Upvote 0
Re: Help to check file name in foder and remove similar

Glad to help you, thanks for the feedback.
 
Upvote 0

Forum statistics

Threads
1,223,894
Messages
6,175,252
Members
452,623
Latest member
Techenthusiast

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