Click to See Complete Forum and Search --> : How to copy files


February 10th, 2000, 04:20 PM
How do I copy a file from one directory to another? It seems that the SHELL command only execute ".exe" type of files. Thus, copy does not work for me.
Thanks in advance.

almike
February 10th, 2000, 04:55 PM
Use the VB FileSystemObject method CopyFile

Dim fso as New FileSystemObject (note that the Microsoft Scripting Runtime library must be referenced in your project)

fso.CopyFile "from path" "to path"

hope that helps

PSH

Johnny101
February 10th, 2000, 04:56 PM
There are a couple of ways to do this:
1)

Sub CopyFiles(SourceFile as string, TargetFile as string)
FileCopy SourceFile, TargetFile
End Sub



That will work in native VB without setting any references. SourceFile and Target must be full paths to the file.
2) Set a reference to the Microsoft Scripting Runtime

Sub CopyFiles(SourceFile as string, TargetFile as string)
Dim FSO as Scripting.FileSystemObject

set FSO = new Scripting.FileSystemObject
'CopyFile (Source, Target, Overwrite Existing as Boolean)
FSO.CopyFile SourceFile, TargetFile, true

set FSO = nothing

End Sub



I like the second way better because I can specify if I want the file overwritten if it already exists, you don't have that option in the first way.

Hope this helps,
John

John Pirkey
MCSD
www.ShallowWaterSystems.com