Help!!! MoveFile Method

C

CAM

Hello,

Can someone see my code and try to help me get rid of this error message:
Run Time error '58';
File already exists

There will be existing files and I do want to overwrite those files
automatically and continue to move the rest of the files to the destination
folder.


Private Sub cmdMoveFiles_Click()
Dim FSO As Object
Dim FromPath As String
Dim ToPath As String
Dim FileExt As String
Dim FNames As String

FromPath = "D:\Main Folder\"
FileExt = "*.xls"

If Right(FromPath, 1) <> "\" Then
FromPath = FromPath & "\"
End If

FNames = Dir(FromPath & FileExt)
If Len(FNames) = 0 Then
MsgBox "There is no files in " & FromPath Exit Sub
End If


Set FSO = CreateObject("scripting.filesystemobject")

ToPath = "D:\Test"

FSO.movefile Source:=FromPath & FileExt, Destination:=ToPath
MsgBox "Files moved from " & FromPath & " into " & ToPath

End Sub
 
J

Jim Cone

You might try copying the file to the destination,
with the "overwrite" argument set to True.
Then delete the original file.
--
Jim Cone
San Francisco, USA
http://www.realezsites.com/bus/primitivesoftware


"CAM" <[email protected]>
wrote in message
Hello,
Can someone see my code and try to help me get rid of this error message:
Run Time error '58';
File already exists
There will be existing files and I do want to overwrite those files
automatically and continue to move the rest of the files to the destination
folder.

Private Sub cmdMoveFiles_Click()
Dim FSO As Object
Dim FromPath As String
Dim ToPath As String
Dim FileExt As String
Dim FNames As String

FromPath = "D:\Main Folder\"
FileExt = "*.xls"

If Right(FromPath, 1) <> "\" Then
FromPath = FromPath & "\"
End If

FNames = Dir(FromPath & FileExt)
If Len(FNames) = 0 Then
MsgBox "There is no files in " & FromPath Exit Sub
End If

Set FSO = CreateObject("scripting.filesystemobject")
ToPath = "D:\Test"
FSO.movefile Source:=FromPath & FileExt, Destination:=ToPath
MsgBox "Files moved from " & FromPath & " into " & ToPath
End Sub
 
C

CAM

Ron,

I really appreciate you help and your excellent programing. Thanks again.
Regards,
 

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

Top