Coping Mulitiple files

Posted on 1998-10-27
Medium Priority
Last Modified: 2011-09-20
How can I copy all the files in a directory to disk a in vb
copy (mydir\*.* a:\
thank you
Question by:sear

Accepted Solution

gencross earned 60 total points
ID: 1441908
This is not an easy thing to do with just a few lines of code.  Here is a routine that will do it...

Right now I don't have it even calling the sParsePath routine and it works, but you can play around with it and modify it if you need to.

If this code works for you I would appreciate an upage of points.  Thanks.

Example of how to call it...

Private Sub Copy_Click()

    CopyDir "F:\sourcedir", "F:\destdir"
End Sub

Function CopyDir(sSourceDir As String, sDestDir As String) As Integer

    CopyTheDirectory = True
    On Error GoTo DirCopy_err
    Dim SourceFile As String
    Dim DestinationFile As String
    '     'Endure src and dest directories have trailing slash
    If Right(sSourceDir, 1) <> "\" Then sSourceDir = sSourceDir & "\"

    If Right(sDestDir, 1) <> "\" Then sDestDir = sDestDir & "\"
    'Call dir for the first file
    SourceFile = Dir(sSourceDir & "*.*") 'returns first file matching *.*
    'Keep calling Dir again without arguments to return the
    'next file in the same directory.

    Do Until SourceFile = ""
           DestinationFile = sDestDir & SourceFile 'sParsePath(SourceFile)
           FileCopy sSourceDir & SourceFile, DestinationFile
           SourceFile = Dir

    Exit Function
    Dim sErrMsg As String
    Select Case Err
    Case 61
    sErrMsg = "Disk Full"
    Case 71
    sErrMsg = "Disk Not Ready"
    Case 76
    sErrMsg = "Path not found"
    Case 70
    sErrMsg = "Permission denied"
End Select

    MsgBox sErrMsg, 48, "Cannot Copy File(s)!"
    CopyTheDirectory = False 'Something failed
End Function

Function sParsePath(sPathIn As String) As String

    Dim I As Integer

    For I = Len(sPathIn) To 1 Step -1
            If InStr(":\", Mid$(sPathIn, I, 1)) Then Exit For

    sParsePath = Left$(sPathIn, I)
End Function


Author Comment

ID: 1441909
I finally figured it out the following short code works fine.
thank you.
Private Sub Command1_Click()
Dim myfile As String
Dim sourcefilename As String
Dim destfilename As String
Dim filenames As New Collection
 myfile = Dir("C:\A\*.*")
 sourcefilename = "c:\a\" & myfile
destfilename = "a:\" & myfile
Do While myfile <> ""
FileCopy sourcefilename, destfilename
myfile = Dir
filenames.Add myfile
End Sub


Featured Post

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.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Most everyone who has done any programming in VB6 knows that you can do something in code like Debug.Print MyVar and that when the program runs from the IDE, the value of MyVar will be displayed in the Immediate Window. Less well known is Debug.Asse…
Background What I'm presenting in this article is the result of 2 conditions in my work area: We have a SQL Server production environment but no development or test environment; andWe have an MS Access front end using tables in SQL Server but we a…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

607 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