Using VBA moving files from one folder to another

gjsala

New Member
Joined
Feb 25, 2003
Messages
34
I would like using VBA to move files from one folder to another. The files moved are time stamped greater than 48 hours old. Any help would be greatly appreciated.

Thanks!
 

Excel Facts

What is =ROMAN(40) in Excel?
The Roman numeral for 40 is XL. Bill "MrExcel" Jelen's 40th book was called MrExcel XL.
Try the following macro, which needs to be placed in a regular module...

Code:
Option Explicit

Sub MoveFiles()

    Dim strFolderA As String
    Dim strFolderB As String
    Dim strFile As String
    Dim Cnt As Long

    '//Change the path to the source folder, accordingly
    strFolderA = "C:\Path\FolderA\"
    
    '//Change the path to the destination folder, accordingly
    strFolderB = "C:\Path\FolderB\"
    
    If Right(strFolderA, 1) <> "\" Then strFolderA = strFolderA & "\"
    If Right(strFolderB, 1) <> "\" Then strFolderB = strFolderB & "\"
    
    '//To filter for .xlsx files, change "*.*" to "*.xlsx"
    strFile = Dir(strFolderA & "*.*")
    
    Do While Len(strFile) > 0
        If Date - Int(FileDateTime(strFolderA & strFile)) > 2 Then
            Cnt = Cnt + 1
            Name strFolderA & strFile As strFolderB & strFile
        End If
        strFile = Dir
    Loop
    
    MsgBox Cnt & " file(s) have been transfered to " & strFolderB, vbInformation
        
End Sub
 
Upvote 0
Actually, the macro selects files where the difference between today's date (without the time) and the file's date (without the time) is greater than 2 days. If you want to select files where you'd like to take the actual time of day into consideration, replace...

Code:
If Date - Int(FileDateTime(strFolderA & strFile)) > 2 Then

with

Code:
If (Date + Time) - FileDateTime(strFolderA & strFile) > 2 Then
 
Upvote 0
Do you know how to have the loop continue before the file has finished moving?

I would like to speed up the process by having the code advance as soon as the command is given.
 
Upvote 0
Try the following macro, which needs to be placed in a regular module...

Code:
Option Explicit

Sub MoveFiles()

    Dim strFolderA As String
    Dim strFolderB As String
    Dim strFile As String
    Dim Cnt As Long

    '//Change the path to the source folder, accordingly
    strFolderA = "C:\Path\FolderA\"
    
    '//Change the path to the destination folder, accordingly
    strFolderB = "C:\Path\FolderB\"
    
    If Right(strFolderA, 1) <> "\" Then strFolderA = strFolderA & "\"
    If Right(strFolderB, 1) <> "\" Then strFolderB = strFolderB & "\"
    
    '//To filter for .xlsx files, change "*.*" to "*.xlsx"
    strFile = Dir(strFolderA & "*.*")
    
    Do While Len(strFile) > 0
        If Date - Int(FileDateTime(strFolderA & strFile)) > 2 Then
            Cnt = Cnt + 1
            Name strFolderA & strFile As strFolderB & strFile
        End If
        strFile = Dir
    Loop
    
    MsgBox Cnt & " file(s) have been transfered to " & strFolderB, vbInformation
        
End Sub

Hi,

How would you change this code so it copies a file from one location to another & re-names it ?

File.xls from C:\test to 'File_.xls' at C:\Done

Please ?

Excel noob here - ta
 
Upvote 0

Forum statistics

Threads
1,223,912
Messages
6,175,341
Members
452,638
Latest member
Oluwabukunmi

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