copy files

  • Thread starter Thread starter Young-Hwan Choi
  • Start date Start date
Y

Young-Hwan Choi

Hi.

How to write a code to copy all the ".xls" files in D:\1 to D:\2 ?

thanks.
 
Young-Hwan,

This is one way...
You can use code from the Microsoft Scripting Runtime Library.
It is included in all recent versions of windows.
Please try this first on files you don't care about.
If it throws an error there is no undo.
"-----------------------------------------------

'Requires reference to "Microsoft Scripting Runtime" in the VBA project.
'Assumes "1" and "2" are folders.
'By replacing "CopyFile" with "MoveFile" you can do just that.
'Jim Cone, June 13, 2004

Sub CopyFilesWithScriptingRuntime()
Dim strFromPath As String
Dim strToPath As String

Dim Fso As Scripting.FileSystemObject
Set Fso = New Scripting.FileSystemObject

'Wildcards only allowed in the "from" path and only at the end.
strFromPath = "D:\1\*.xls"
strToPath = "D:\2"

'False prevents overwriting of files
Fso.CopyFile strFromPath, strToPath, False

'Technically not required, but I prefer it.
Set Fso = Nothing
End Sub
'-----------------------------------------------
Regards,
Jim Cone
San Francisco, CA
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top