Home
Search
 
What's New
Index
Books
Links
Q & A
Newsletter
Banners
 
Feedback
Tip Jar
 
C# Helper...
 
XML RSS Feed
Follow VBHelper on Twitter
 
 
 
MSDN Visual Basic Community
 
 
 
 
 
 
TitleCopy files in a directory that match a pattern using Dir and FileCopy
Description
KeywordsDir, FileCopy, pattern, copy, files
CategoriesFiles and Directories
 
Use Dir to list the files and FileCopy to copy them.
 
Private Sub cmdCopy_Click()
    source_path = txtSourceDir.Text
    If Right$(source_path, 1) <> "\" Then source_path = _
        source_path & "\"
    dest_path = txtDestDir.Text
    If Right$(dest_path, 1) <> "\" Then dest_path = _
        dest_path & "\"

    ' Get the first file name.
    file_name = Dir$(source_path & txtPattern.Text)

    ' Repeat until Dir returns no more files.
    Do While Len(file_name) > 0
        ' Copy this file.
        num_files = num_files + 1
        FileCopy source_path & file_name, dest_path & _
            file_name

        ' Get the next file.
        file_name = Dir$()
    Loop

    MsgBox "Copied " & Format$(num_files) & " files."
End Sub

Private Sub Form_Load()
    file_path = App.Path
    If Right$(file_path, 1) <> "\" Then file_path = _
        file_path & "\"
    txtSourceDir.Text = file_path
    txtDestDir.Text = file_path & "Test"

    ' Make a test destination directory.
    ' Don't do this if you want to send the files
    ' somewhere else!
    On Error Resume Next
    MkDir file_path & "Test"
    On Error GoTo 0
End Sub
 
Formatted by Neil Crosby
 
 
Copyright © 1997-2010 Rocky Mountain Computer Consulting, Inc.   All rights reserved.
  Updated