Solved

Coping Mulitiple files

Posted on 1998-10-27
2
140 Views
Last Modified: 2011-09-20
How can I copy all the files in a directory to disk a in vb
example:
copy (mydir\*.* a:\
thank you
0
Comment
Question by:sear
2 Comments
 
LVL 4

Accepted Solution

by:
gencross earned 20 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
    Loop

ExitHere:
    Exit Function
DirCopy_err:
    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
    Next

    sParsePath = Left$(sPathIn, I)
   
End Function


0
 

Author Comment

by:sear
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
Loop
End Sub

0

Featured Post

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

There are many ways to remove duplicate entries in an SQL or Access database. Most make you temporarily insert an ID field, make a temp table and copy data back and forth, and/or are slow. Here is an easy way in VB6 using ADO to remove duplicate row…
Introduction While answering a recent question about filtering a custom class collection, I realized that this could be accomplished with very little code by using the ScriptControl (SC) library.  This article will introduce you to the SC library a…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

810 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