[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 146
  • Last Modified:

Coping Mulitiple files

How can I copy all the files in a directory to disk a in vb
example:
copy (mydir\*.* a:\
thank you
0
sear
Asked:
sear
1 Solution
 
gencrossCommented:
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
 
searAuthor Commented:
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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now