Link to home
Start Free TrialLog in
Avatar of KingSencat
KingSencat

asked on

Moving Files To Onother Folder ( Visual Basic v6.0 )

I already use a code to move all the files from c:\test\ to c:\test\test , however if the files already exist on c:\test\test i get error that file names already exist, how do i overwrite that files? Here is the code i use. Thanks
Dim MyOldpath As String, MyNewpath As String, MyName As String
MyOldpath = "c:\test\"' ' Set the path
MyNewpath = "c:\test\test\" ' The new path
MyName = Dir(MyOldpath, vbNormal)    ' Retrieve the first entry.
Do While MyName <> ""    ' Start the loop.
    Name MyOldpath & MyName As MyNewpath & MyName ' This moves the files from MyOldPath to MyNewPath
    MyName = Dir    ' Get next entry.
    Loop

Open in new window

Avatar of Antagony1960
Antagony1960

You have to copy each file with FileCopy then delete the original.

Although personally I think the native file commands in VB6 are not particularly good or intuitive; I much prefer using the FileSystemObject. If you want to use that, add a reference (Project|References...) to the Microsoft Scripting Runtime and use it like this:
Dim FSO As New FileSystemObject, oFolder As Folder, oFile As File
Dim MyOldpath As String, MyNewpath As String
    MyOldpath = "c:\test\" ' Set the path'
    MyNewpath = "c:\test\test\" ' The new path'
    Set oFolder = FSO.GetFolder(MyOldpath)
    For Each oFile In oFolder.Files
        oFile.Copy MyNewpath, True
        oFile.Delete True
    Next
    Set oFile = Nothing
    Set oFolder = Nothing
    Set FSO = Nothing

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of GrahamSkan
GrahamSkan
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial