Option Explicit
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' Copyright ©1996-2011 VBnet/Randy Birch, All Rights Reserved.
' Some pages may also contain other copyrights by the author.
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' Distribution: You can freely use this code in your own
' applications, but you may not reproduce
' or publish this code on any web site,
' online service, or distribute as source
' on any media without express permission.
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
PrivateConst INVALID_HANDLE_VALUE = -1
PrivateConst MAX_PATH As Long = 260
PrivateType FILETIME
dwLowDateTime As Long
dwHighDateTime As Long
End Type
PrivateType WIN32_FIND_DATA
dwFileAttributes As Long
ftCreationTime As FILETIME
ftLastAccessTime As FILETIME
ftLastWriteTime As FILETIME
nFileSizeHigh As Long
nFileSizeLow As Long
dwReserved0 As Long
dwReserved1 As Long
cFileName As String * MAX_PATH
cAlternate As String * 14
End Type
PrivateType SECURITY_ATTRIBUTES
nLength As Long
lpSecurityDescriptor As Long
bInheritHandle As Long
End Type
PrivateDeclare Function CreateDirectory Lib "kernel32" _
Alias "CreateDirectoryA" _
(ByVal lpPathName As String, _
lpSecurityAttributes As SECURITY_ATTRIBUTES) As Long
PrivateDeclare Function CopyFile Lib "kernel32" _
Alias "CopyFileA" _
(ByVal lpExistingFileName As String, _
ByVal lpNewFileName As String, _
ByVal bFailIfExists As Long) As Long
PrivateDeclare Function FindFirstFile Lib "kernel32" _
Alias "FindFirstFileA" _
(ByVal lpFileName As String, _
lpFindFileData As WIN32_FIND_DATA) As Long
PrivateDeclare Function FindNextFile Lib "kernel32" _
Alias "FindNextFileA" _
(ByVal hFindFile As Long, _
lpFindFileData As WIN32_FIND_DATA) As Long
PrivateDeclare Function FindClose Lib "kernel32" _
(ByVal hFindFile As Long) As Long
Private Sub Command1_Click()
Dim sSourcePath As String
Dim sDestination As String
Dim sFiles As String
Dim numCopied As Long
'set the appropriate initializing values
sSourcePath = "c:\win\"
sDestination = "c:\temptest\"
sFiles = "*.txt"
'perform the copy and return the copied file count
numCopied = rgbCopyFiles(sSourcePath, sDestination, sFiles)
MsgBox numCopied & " files copied to " & sDestination
End Sub
Private Sub Command2_Click()
Unload Me
End Sub
PrivateFunction rgbCopyFiles(sSourcePath As String, _
sDestination As String, _
sFiles As String) As Long
Dim WFD As WIN32_FIND_DATA
Dim SA As SECURITY_ATTRIBUTES
Dim r As Long
Dim hFile As Long
Dim bNext As Long
Dim copied As Long
Dim currFile As String
'Create the target directory if it doesn't exist
Call CreateDirectory(sDestination, SA)
'Start searching for files in the Target directory.
hFile = FindFirstFile(sSourcePath & sFiles, WFD)
If (hFile = INVALID_HANDLE_VALUE) Then
'nothing to do, so bail out
MsgBox "No " & sFiles & " files found."
Exit Function
End If
'Copy each file to the new directory
If hFile Then
Do
'trim trailing nulls, leaving one to terminate the string
currFile = Left$(WFD.cFileName, InStr(WFD.cFileName, Chr$(0)))
'copy the file to the destination directory & increment the count
Call CopyFile(sSourcePath & currFile, sDestination & currFile, False)
copied = copied + 1
'just to check what's happening
List1.AddItem sSourcePath & currFile
'find the next file matching the initial file spec
bNext = FindNextFile(hFile, WFD)
Loop Until bNext = 0
End If
'Close the search handle
Call FindClose(hFile)
'and return the number of files copied
rgbCopyFiles = copied
End Function |