Solved

VB6 copy and move

Posted on 2009-07-06
7
314 Views
Last Modified: 2012-05-07
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

0
Comment
Question by:HKFuey
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
7 Comments
 
LVL 10

Expert Comment

by:peetm
ID: 24784142
0
 

Author Comment

by:HKFuey
ID: 24784238
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
 

Author Comment

by:HKFuey
ID: 24784316
OK got a bit further, I added the reference Microsoft Scripting Runtime
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:HKFuey
ID: 24784372
I can get the FilesystemObject to work, does anyone know the syntax to copy/move all the files in a folder?
0
 

Author Comment

by:HKFuey
ID: 24784490
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
 
LVL 4

Accepted Solution

by:
De_Wim99 earned 500 total points
ID: 24785013
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
 

Author Closing Comment

by:HKFuey
ID: 31600099
Thanks for the help!
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
passing parameter in sql procedure 9 70
Dinamic report to Crosstab query 9 60
VBA Replace Function - What am I missing? 3 52
Userform to show a range in excel. 3 34
When trying to find the cause of a problem in VBA or VB6 it's often valuable to know what procedures were executed prior to the error. You can use the Call Stack for that but it is often inadequate because it may show procedures you aren't intereste…
When designing a form there are several BorderStyles to choose from, all of which can be classified as either 'Fixed' or 'Sizable' and I'd guess that 'Fixed Single' or one of the other fixed types is the most popular choice. I assume it's the most p…
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…

751 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