Filecopy For transfering files

bradyboyy88

Well-known Member
Joined
Feb 25, 2015
Messages
562
Hi,

I was wondering if anyone knows if the macro continues when you use filecopy or does the macro wait for the file to finish copying before moving on to the next line of code?
 

Excel Facts

Control Word Wrap
Press Alt+Enter to move to a new row in a cell. Lets you control where the words wrap.
AFAIK, the vb(a) as well as the FileSystemObject FileCopying operations are both Synchronous so calling code will wait if a large file is being copied.

If you want the code not to wait, you could performe the copying from the commandline via the use of the Shell Function.. Something along these lines :

Code:
Sub test()

    Dim sSourceFile As String, sDestFile As String
    
    sSourceFile = "C:\Users\File.mp4"
    sDestFile = "C:\Users\Test\File.mp4"
    
[COLOR=#008000][B]    'Asynch File Copy[/B][/COLOR]
    Shell "xcopy " & sSourceFile & " " & sDestFile & "*", vbHide
    
[B][COLOR=#008000]    ' Other code resumes here immediatly without waiting for the file copy operation to finish.[/COLOR][/B]
    
    Debug.Print "resuming code"


End Sub
 
Last edited:
Upvote 0
For some reason this is not doing anything for me. Its not drawing any errors just not copying the file. Any ideas?
 
Upvote 0
Did a quick MrExcel search on executing DOS commands like xcopy and found a nice explanation by our member Rick Rothstein here: shell commands in excel

Based on that, I gave the following a shot and successfully copied the file MyVideo.MP4 from the RAX10 folder onto the Test folder ... change the paths as required.

Code:
Sub Test()
    Dim source As String, Destination As String
    
    source = "C:\Users\Info-Hp\Desktop\RAX10\MyVideo.MP4"
    Destination = "C:\Users\Info-Hp\Desktop\Test\"
    Shell Environ$("comspec") & " /c xcopy """ & source & """ """ & Destination & """ ", vbHide
End Sub
 
Upvote 0
Works like a charm so thanks, does xcode prevent from being able to rename it on transfer similar to filecopy?

Basically what my code is doing with filecopy is taking the file and copying it to a new directory but renaming it with a standard convention (which we track in a database basicallly). So renaming it on transfer is kind of important. I am looking to see if this is possible with filesystemobject but it looks like you still have to wait till it transfers.
 
Last edited:
Upvote 0
I couldn't make it rename the copied file .. so I am not sure really.

I guess you could use Dir inside a loop to keep checking until the file is finally copied and then you can rename it using Name Src As Des ..
 
Last edited:
Upvote 0
Yea so adding * to the destination name allows you to rename it. Your code works great with that addition!
 
Upvote 0

Forum statistics

Threads
1,223,909
Messages
6,175,310
Members
452,634
Latest member
cpostell

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