VB6 copy and move

I have some VB6 code that copies files over the network.
I want to use this to copy SQL backup files which are in this format: - Database_backup_200906301200.bak
Does anyone know of a simple solution that allows me to copy all the items in a folder to another and then move the copied files so they are not re-copied next time the program is run?
Option Explicit
Dim NetFile As String
Dim HDFile As String
 
 
Sub main(HDFile As String, NetFile As String)
 
    On Error GoTo Errtrap
    FileCopy NetFile, HDFile
    
    Exit Sub
Errtrap:
    Select Case Err
        Case 91 ' not set
        Resume Next
        Case 53 ' file not found
        Resume Next
 
    Case Else
        MsgBox Err & "  " & Err.Description
 
        End
        Exit Sub
    End Select
End Sub
 
Private Sub Form_Load()
 
    NetFile = "\\files\users\andy\temp\Test.bak"
    HDFile = "c:\temp\test.bak"
    
    main HDFile, NetFile
    End
    
End Sub

Open in new window

HKFueyAsked:
Who is Participating?
 
WimConnect With a Mentor Commented:
not tested, but this should do the trick:
Option Explicit
 
Sub main(HDFile As String, NetFile As String, MovFile As String)
Dim fs As Scripting.FileSystemObject
Dim scrFolder As Scripting.Folder
Dim scrFile As Scripting.File
Set fs = New Scripting.FileSystemObject
Set scrFolder = fs.GetFolder(HDFile)
 
    On Error GoTo Errtrap
 
For Each scrFile In scrFolder.Files
     fs.CopyFile NetFile, HDFile & scrFile.Name
     fs.MoveFile NetFile, MovFile & scrFile.Name
Next
 
Set fs = Nothing
Set scrFolder = Nothing
Set scrFile = Nothing
    
    Exit Sub
Errtrap:
    Select Case Err
        Case 91 ' not set
        Resume Next
        Case 53 ' file not found
        Resume Next
 
    Case Else
        MsgBox Err & "  " & Err.Description
        Set fs = Nothing
        Set scrFolder = Nothing
        Set scrFile = Nothing
        End
        Exit Sub
    End Select
End Sub
 
Private Sub Form_Load()
    Dim MovFile As String
    Dim NetFile As String
    Dim HDFile As String
    
    NetFile = "\\files\users\andy\temp\"
    HDFile = "c:\temp2\"
    MovFile = "\\files\users\andy\temp\Mov\"
    
    main HDFile, NetFile, MovFile
    End
    
End Sub

Open in new window

0
 
peetmCommented:
0
 
HKFueyAuthor Commented:
I have already looked at that here: -
http://articles.techrepublic.com.com/5100-10878_11-1050078.html (posted by Angelll)
If I put this in my code 'Dim fso As New FileSystemObject' I get an error 'User Type not defined'
0
The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

 
HKFueyAuthor Commented:
OK got a bit further, I added the reference Microsoft Scripting Runtime
0
 
HKFueyAuthor Commented:
I can get the FilesystemObject to work, does anyone know the syntax to copy/move all the files in a folder?
0
 
HKFueyAuthor Commented:
OK I've done it, code below

Option Explicit
Dim MovFile
Dim NetFile As String
Dim HDFile As String
 
 
 
Sub main(HDFile As String, NetFile As String)
Dim fso As New FileSystemObject
 
    On Error GoTo Errtrap
 
    fso.CopyFile NetFile, HDFile
    fso.MoveFile NetFile, MovFile
    
    Exit Sub
Errtrap:
    Select Case Err
        Case 91 ' not set
        Resume Next
        Case 53 ' file not found
        Resume Next
 
    Case Else
        MsgBox Err & "  " & Err.Description
 
        End
        Exit Sub
    End Select
End Sub
 
Private Sub Form_Load()
 
    NetFile = "\\files\users\andy\temp\*.*"
    HDFile = "c:\temp2\"
    MovFile = "\\files\users\andy\temp\Mov\"
    
    main HDFile, NetFile
    End
    
End Sub

Open in new window

0
 
HKFueyAuthor Commented:
Thanks for the help!
0
All Courses

From novice to tech pro — start learning today.