Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Help moving and copying files

Status
Not open for further replies.

EarS

Programmer
Jun 5, 2002
26
US
Does anyone know where I can find information to move and copy files programatically in VBA? I have found lots of information on the net but none of the VB code seems to want to compile in VBA. Any help is greatly appreciated!

Thanks

Matt
 
Hi, to move object use the Name command

Name FilePath1 As FilePath2

To Copy a file use the CopyFile method of the FileSystemObject

Dim fso As Object
Set fso = CreateObject("Scripting.FileSystemObject")
fso.copyfile FileName1, FileName2
 
One way I know of is to use the Microsoft Scripting Runtime reference and use VBScript to do it.

Dim fso As FileSystemObject
Set fso = CreateObject("Scripting.FileSystemObject")
objScript.CopyFile <source>, <destination>, True

I'm guessing that the True means overwrite.

You can probably do it with ShellExecute or Shell as well.
 
Oops! I was supposed to change objScript in the last post to fso. Looks like someone already posted it, anyway. To move using VBScript, fso.MoveFile ....
 
when I try to use this I get an error that says &quot;file not found&quot; here is the code that I used:

Dim fso As Object
Set fso = CreateObject(&quot;Scripting.FileSystemObject&quot;)
fso.copyfile &quot;c:\CopyFrom&quot;, &quot;c:\CopyTo&quot;

I have confirmed that the paths for both directories are spell correctly. I don't understand what the problem is.

Thanks for all the help...

Matt
 
It has to came from a path error. Don't forget the files extentions.
 
Is there anyway to move and entire directory??? This would be most helpful.

Thanks for all the help.

Matt
 
Try this EarS :

Dim fso As Object
Set fso = CreateObject(&quot;scripting.filesystemobject&quot;)
fso.movefile &quot;c:\files\*.*&quot;, &quot;c:\movedfiles\&quot;
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top