ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   copy files (https://www.excelbanter.com/excel-programming/301291-copy-files.html)

Young-Hwan Choi

copy files
 
Hi.

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

thanks.



Jim Cone

copy files
 
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


"Young-Hwan Choi" wrote in message ...
Hi.
How to write a code to copy all the ".xls" files in D:\1 to D:\2 ?
thanks.




All times are GMT +1. The time now is 06:15 PM.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
ExcelBanter.com