• Hi All

    Please note that at the Chandoo.org Forums there is Zero Tolerance to Spam

    Post Spam and you Will Be Deleted as a User

    Hui...

  • When starting a new post, to receive a quicker and more targeted answer, Please include a sample file in the initial post.

Alter Code that Kill all old files leaving newest file to move old files to folder

I have a folder with multiple files of different names and all have multiple copies differing by date and time they were generated, the macro kills all the files leaving only the newest files of any name

In the folder I have another folder called OldFiles I need to move the old files to this folder instead of killing them, I have tried but can not get how to do this

Thanks

Code:
Sub DirKiller()
Dim f As String, FName As String, K, N As Long, S As String

f = ThisWorkbook.path & "\Files to Combine\": FName = dir(f)
RunDirectory: If FName = "" Then GoTo Killem
              S = S & FName
              S = S & vbCrLf
              FName = dir(): GoTo RunDirectory
                       
Killem: K = Split(S, vbCrLf)
DownBubble: For N = LBound(K) To UBound(K) - 1
            If K(N + 1) > K(N) Then
            S = K(N): K(N) = K(N + 1): K(N + 1) = S
            GoTo DownBubble: End If: Next N: S = K(0)
                   
            For N = 1 To UBound(K)
Garrotte: If N > UBound(K) Then Exit Sub
            If Len(S) = Len(K(N)) Then
                Kill f & K(N)
            Else:
                S = K(N): N = N + 1: GoTo Garrotte

            End If: Next N
End Sub
 
I got it

K(N) is the file names to be killed or moved

Replace
Code:
 Kill f & K(N)

with

Code:
 Name ThisWorkbook.path & "\MyFolder\" & K(N) As ThisWorkbook.path & "\MyFolder\Old Media & Codes Files\" & K(N)
 
Back
Top