Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

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

Posted on 2009-02-24
2
Medium Priority
?
216 Views
Last Modified: 2012-05-06
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

0
Comment
Question by:KingSencat
2 Comments
 
LVL 11

Expert Comment

by:Antagony1960
ID: 23723843
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

0
 
LVL 77

Accepted Solution

by:
GrahamSkan earned 2000 total points
ID: 23723946
You can check the presence of the file with Dir() and delete (Kill) it, but if that is done in the Dir loop, it will stop the loop operating, so you can collect the names into an array.
Sub MoveFiles()
    Dim MyOldpath As String, MyNewpath As String, MyName As String
    Dim strFiles() As String
    Dim i As Integer
    Dim j As Integer
    
    MyOldpath = "c:\test\" ' ' Set the path
    MyNewpath = "c:\test\test\" ' The new path
    'Collect the file names
    MyName = Dir(MyOldpath, vbNormal)    ' Retrieve the first entry.
    Do While MyName <> ""    ' Start the loop.
        ReDim strFiles(i)
        strFiles(i) = MyName
        i = i + 1
    Loop
    'Move files in the array
    For j = 0 To i - 1
        If Dir(MyNewpath & strFiles(j)) <> "" Then
            Kill MyNewpath & strFiles(j)
        End If
        Name MyOldpath & strFiles(j) As MyNewpath & strFiles(j) ' This moves the files from MyOldPath to MyNewPath
        strFiles(j) = Dir    ' Get next entry.
    Next j
End Sub

Open in new window

0

Featured Post

Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Have you ever wanted to restrict the users input in a textbox to numbers, and while doing that make sure that they can't 'cheat' by pasting in non-numeric text? Of course you can do that with code you write yourself but it's tedious and error-prone …
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…

572 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question