' Copy all files below this directory. Return the
' number of files copied.
Private Function CopyFiles(ByVal from_dir As String, ByVal _
to_dir As String) As Long
Dim files_copied As Long
Dim dirs As Collection
Dim fname As String
Dim search_handle As Long
Dim file_data As WIN32_FIND_DATA
Dim i As Integer
Set dirs = New Collection
' Get the first file.
search_handle = FindFirstFile( _
from_dir & "*.*", file_data)
If search_handle <> INVALID_HANDLE_VALUE Then
' Get the rest of the files.
Do
' Get the file name.
fname = file_data.cFileName
fname = Left$(fname, InStr(fname, Chr$(0)) - 1)
' Skip the files "." and "..".
If fname <> "." And fname <> ".." Then
files_copied = files_copied + 1
' See if the file is a directory.
If file_data.dwFileAttributes And _
DDL_DIRECTORY Then
' This is a directory.
' Make the new directory.
MkDir to_dir & fname
' Save the directory name so
' we can search it later.
dirs.Add fname
Else
' This is not a directory.
' Copy the file.
FileCopy from_dir & fname, to_dir & _
fname
End If
End If
' Get the next file.
If FindNextFile(search_handle, file_data) = 0 _
Then Exit Do
Loop
' Close the file search hanlde.
FindClose search_handle
End If
' Search subdirectories.
For i = 1 To dirs.Count
fname = dirs(i)
files_copied = files_copied + CopyFiles(from_dir & _
fname & "\", to_dir & fname & "\")
Next i
CopyFiles = files_copied
End Function
' Copy from_file to to_file. If from_file is a
' directory, copy it and its files, creating to_file
' if necessary.
Private Function XCopyFile(ByVal from_file As String, ByVal _
to_file As String) As Long
Dim files_copied As Long
' See if from_file is a directory.
If GetAttr(from_file) And vbDirectory Then
' This is a directory.
If Right$(from_file, 1) <> "\" Then from_file = _
from_file & "\"
If Right$(to_file, 1) <> "\" Then to_file = to_file _
& "\"
' Create to_file if necessary.
On Error Resume Next
MkDir to_file
If Err.Number = 0 Then files_copied = 1
On Error GoTo 0
' Copy the files in the directory.
files_copied = files_copied + CopyFiles(from_file, _
to_file)
Else
' This is not a directory.
' Copy the file.
FileCopy from_file, to_file
files_copied = 1
End If
XCopyFile = files_copied
End Function
|