VBA to move files (duplicated filename handling)

Dampa88

Board Regular
Joined
Apr 28, 2016
Messages
53
Office Version
  1. 365
Platform
  1. Windows
Dears,

I have a macro that is opening all txt file from a specified folder, do some actions in Excel and then move them to a destination folder.

I'd like to avoid possible errors in case the file already exists in the destination folder.
To move the files I use File.Move which I guess has no feature to handle this situation.

I though to rename the file in the loop adding the timestamp at the end, but my VBA knowledge is still quite limited.

VBA Code:
Sub Test()

    Dim orig_path As String, path As String, MyFile As Variant, a As Variant, count As Integer

    orig_path = "C:\AAA"
    dest_path = "C:\BBB"
    
    With CreateObject("scripting.filesystemobject")
     For Each MyFile In .GetFolder(orig_path).Files
       If .GetExtensionName(MyFile) = "txt" Then

        'long code here to do open txt file, import in excel, etc.

        MyFile.Move (dest_path & "\" & MyFile.Name)
        End If
      Next
    End With
   
End Sub

Do you have some advices for me?

Thanks,
D
 

Excel Facts

What is the last column in Excel?
Excel columns run from A to Z, AA to AZ, AAA to XFD. The last column is XFD.
I'm not sure I understand what you are asking. Are you having some problem with the code you posted? If so, can you explain further?
 
Upvote 0
Hi rlv01, yes I'm having some problems with the code.

For example, let's assume that I have two files called Test1.txt and Test2.txt in the folder C:\AAA

The first run of the macro will be fine, the txt files will be elaborated and then moved to C:\BBB
For the future run, if I will have again a file named Test1.txt in the folder C:\AAA, I will get an error when it tries to move it to the destination folder, as a same file already exists.

For this reason, before moving the file I was thinking to put a timestamp, so that in the destination folder I'll have:
Test1 20230522130105.txt
Test2 20230522130105.txt
Test1 20230523125107.txt
Etc.

I hope it's clearer now, thanks!
 
Upvote 0
For this reason, before moving the file I was thinking to put a timestamp
Or you could overwrite any existing file with the new file. Is there a reason you don't want to do that?
 
Upvote 0
Yes, I need to keep history of the files, duplicated ones included.
 
Upvote 0
Not tested.

VBA Code:
Sub Test()

    Dim orig_path As String, path As String, a As Variant, count As Integer
    Dim dest_path As String, NewName As String
    Dim MyFile As Object

    orig_path = "C:\AAA"
    dest_path = "C:\BBB"

    With CreateObject("scripting.filesystemobject")
        For Each MyFile In .GetFolder(orig_path).Files
            If .GetExtensionName(MyFile) = "txt" Then

                'long code here to do open txt file, import in excel, etc.

                If .FolderExists(dest_path) Then
                    NewName = dest_path & "\" & .GetBaseName(MyFile.Name) & Format(Date, "yyyymmdd") & Format(Time, "hhmmss") & ".txt"
                    .CopyFile MyFile.path, NewName, True
                End If
            End If
        Next
    End With
End Sub
 
Upvote 1
Solution
Tested and works perfectly! Thanks a lot!
I've just added a .DeleteFile MyFile.path so that files are deleted from older folder.
 
Upvote 0

Forum statistics

Threads
1,223,910
Messages
6,175,320
Members
452,635
Latest member
laura12345

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