file copy using listbox

ealand3
ealand3 used Ask the Experts™
on
i am using a search to add file names to a list box and then copy those files to a different directory i got it to work with one problem it copys all files listed with the exception of one i am assuming it is missing the first file but not sure the code i am using s a modified version of sample code off the msdn tips nd tricks:
******************************************************************************************************************
Dim XFilename As String
Dim StrtPath As String
Dim X As Long
Dim y As Long
Dim queryfiles As String
Dim SourceDir As String
Dim TargetDir As String

Private Sub Command1_Click()
    X = Val(txtBeg.Text)
    y = Val(txtEnd.Text)
Dim XLen As Integer
   Dim i As Integer
   
    For i = X To y
   
    queryfiles = "SELECT Table1.* From Table1 WHERE (((Table1.id)= " & Str(i) & "));"
    datFiles.RecordSource = queryfiles
    datFiles.Refresh




   
   
    XLen = Len(Text1.Text)
   
    If Mid$(Text1.Text, XLen, 1) = "\" Then
        StrtPath = Left$(Text1.Text, XLen - 1)
    Else
        StrtPath = Text1.Text
    End If
   
    XFilename = lblLast.Caption & lblFirst.Caption & "*"
   
    List1.Clear
    List2.Clear
   
    List1.AddItem StrtPath
    Get_Files StrtPath
    List_Files
     
     
Next i
End Sub








Private Sub Get_Files(FPath As String)
    Dim File_Name As String
    Dim File_Path As String
    Dim File_Read As Integer
    Dim X As Boolean
    Dim i As Integer

    File_Path = FPath & "\"
    File_Name = Dir$(File_Path, vbDirectory)
    File_Read = 1
    X = False

    Do While File_Name <> ""
        If File_Name <> "." And File_Name <> ".." Then
            If GetAttr(File_Path & File_Name) = vbDirectory Then
                StrtPath = File_Path & File_Name
                List1.AddItem StrtPath
                X = True
                Get_Files StrtPath
            End If
        End If
        If X = True Then
            File_Name = Dir$(File_Path, vbDirectory)
            For i = 2 To File_Read
                File_Name = Dir$
            Next
            X = False
        End If
        File_Name = Dir$
        File_Read = File_Read + 1
    Loop
End Sub



Private Sub List_Files()
    Dim XIndex As Integer
    Dim XName As String
    Dim X_Filename As String

    For XIndex = 0 To List1.ListCount - 1
        XName = List1.List(XIndex) & "\" & XFilename
        X_Filename = Dir(XName)
        If X_Filename <> "" Then
            List2.AddItem List1.List(XIndex) & "\" & X_Filename
               
        'if i put a filecopy here i get a runtime error permission denied
                'FileCopy (List1.List(XIndex) & "\" & X_Filename), ("c:\project\data\" & Str(lblId.Caption) & "\" & X_Filename)
               
                Do While True
                    X_Filename = Dir
                    On Error GoTo exit_loop
                        If X_Filename <> "" Then
                            List2.AddItem List1.List(XIndex) & "\" & X_Filename
             'this will copy files but misses one ie.   if  it finds 4 files_
             ' it will copy 3 of them
             
                          FileCopy (List1.List(XIndex) & "\" & X_Filename), ("c:\project\data\" & Str(lblId.Caption) & "\" & X_Filename)
                           
                        Else
                            Exit Do
                        End If
                Loop
        End If
exit_loop:
        Next XIndex
End Sub
******************************************************************************************************************

i need help catching that last(or first) file any ideas would be appreciated
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Ther are a few problems with Dir.  Here is a tip:

Whenever you use Dir pick up all files into an array before before you start to deal with the files:

Example:

Dim FileList as Collection

Set FileList = New Collection
Folder=App.Path+"\Data"
FileName=Dir(Folder + "\*.*")
DO While Len(Filename)>0
    FileList.Add FileName, FileName
    FileName = Dir
Loop


' Now you can work with the files

Dim File

For Each File In FileList
    FileCopy Folder + "\" + File,......
Next
can't use Dir() recursively.
str1 = Dir(strPathFile1)
While str1 <> ""
    ...
    str2 = Dir(strPathFile2)
    While str2 <> ""
        ...
        str2 = Dir() 'you want Dir(strPathFile2) next entry, it right
    Wend
    str1 = Dir() 'you want Dir(strPathFile1) net entry, but it's Dir(strPathFile1) next entry
Wend

you need use a collection save all sub directory in loop, after loop end, use every collection item to call another Dir()

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial