Solved

Coping Mulitiple files

Posted on 1998-10-27
2
138 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

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

Suggested Solutions

Introduction I needed to skip over some file processing within a For...Next loop in some old production code and wished that VB (classic) had a statement that would drop down to the end of the current iteration, bypassing the statements that were c…
Have you ever wanted to restrict the users input in a textbox to numbers, and while doing that make sure that they can't 'cheat' by pasting in non-numeric text? Of course you can do that with code you write yourself but it's tedious and error-prone …
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…
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…

759 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now