[Last Call] Learn about multicloud storage options and how to improve your company's cloud strategy. Register Now

x
?
Solved

Coping Mulitiple files

Posted on 1998-10-27
2
Medium Priority
?
145 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
[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
2 Comments
 
LVL 4

Accepted Solution

by:
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
    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

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

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

Introduction In a recent article (http://www.experts-exchange.com/A_7811-A-Better-Concatenate-Function.html) for the Excel community, I showed an improved version of the Excel Concatenate() function.  While writing that article I realized that no o…
Introduction While answering a recent question (http://www.experts-exchange.com/Q_27402310.html) in the VB classic zone, I wrote some VB code in the (Office) VBA environment, rather than fire up my older PC.  I didn't post completely correct code o…
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…
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…
Suggested Courses

650 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