VBA to copy all but newest .txt file to another location.

JamesRo

New Member
Joined
Jun 30, 2022
Messages
26
Office Version
  1. 2021
Platform
  1. Windows
Hi.
Looking for code to copy / paste all .txt files from a folder to another folder, but leave the newest where it is.

Any help appreciated, thanks.

James.
 

Excel Facts

Excel Can Read to You
Customize Quick Access Toolbar. From All Commands, add Speak Cells or Speak Cells on Enter to QAT. Select cells. Press Speak Cells.
Change the source and destination folders as required.

VBA Code:
Option Explicit

Public Sub Copy_Txt_Files_Except_Newest()

    Dim sourceFolder As String, destinationFolder As String
    Dim file As String
    Dim newestFile As String, newestDate As Date
    
    sourceFolder = "C:\path\to\folder1\"
    destinationFolder = "C:\path\to\folder2\"
    
    If Right(sourceFolder, 1) <> "\" Then sourceFolder = sourceFolder & "\"
    If Right(destinationFolder, 1) <> "\" Then destinationFolder = destinationFolder & "\"
    
    newestDate = 0
    newestFile = ""
    file = Dir(sourceFolder & "*.txt")
    While file <> vbNullString
        FileCopy sourceFolder & file, destinationFolder & file
        If FileDateTime(sourceFolder & file) > newestDate Then
            newestFile = file
            newestDate = FileDateTime(sourceFolder & file)
        End If
        file = Dir
    Wend
    
    If newestFile <> "" Then
        Kill destinationFolder & newestFile
    End If
    
End Sub
 
Upvote 0
Oops sorry, I did not describe that too well.
This works perfectly but you copy all of the files then delete the newest from the destination it looks like.

I want to copy and remove all of the files from the source but not the newest.
TIA.
 
Upvote 0
To move all .txt files except the newest:

VBA Code:
Public Sub Move_Txt_Files_Except_Newest()

    Dim sourceFolder As String, destinationFolder As String
    Dim file As String
    Dim newestFile As String, newestDate As Date
    
    sourceFolder = "C:\path\to\folder1\"
    destinationFolder = "C:\path\to\folder2\"
    
    If Right(sourceFolder, 1) <> "\" Then sourceFolder = sourceFolder & "\"
    If Right(destinationFolder, 1) <> "\" Then destinationFolder = destinationFolder & "\"
    
    newestDate = 0
    newestFile = ""
    file = Dir(sourceFolder & "*.txt")
    While file <> vbNullString
        If FileDateTime(sourceFolder & file) > newestDate Then
            newestFile = file
            newestDate = FileDateTime(sourceFolder & file)
        End If
        Name sourceFolder & file As destinationFolder & file
        file = Dir
    Wend
    
    If newestFile <> "" Then
        Name destinationFolder & newestFile As sourceFolder & newestFile
    End If
    
End Sub
 
Upvote 0

Forum statistics

Threads
1,221,442
Messages
6,159,905
Members
451,601
Latest member
terrynelson55

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