Copiare una cartella, tutte le sottocartelle ed i file in esse contenuti con Visual Basic

Entrambe le funzioni restituiscono il numero dei file copiati. Public Const MAX_PATH = 260 Public Type FILETIME dwLowDateTime As Long dwHighDateTime As Long End Type Public Type WIN32_FIND_DATA dwFileAttributes As Long ftCre...

Entrambe le funzioni restituiscono il numero dei file copiati.

Public Const MAX_PATH = 260
Public Type FILETIME
dwLowDateTime As Long
dwHighDateTime As Long
End Type
Public Type 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

Public Declare Function FindFirstFile Lib “kernel32” Alias “FindFirstFileA” (ByVal lpFileName As String, lpFindFileData As WIN32_FIND_DATA) As Long
Public Declare Function FindNextFile Lib “kernel32” Alias “FindNextFileA” (ByVal hFindFile As Long, lpFindFileData As WIN32_FIND_DATA) As Long
Public Declare Function FindClose Lib “kernel32” (ByVal hFindFile As Long) As Long
Public Declare Function GetLastError Lib “kernel32” () As Long

Public Const ERROR_NO_MORE_FILES = 18&
Public Const INVALID_HANDLE_VALUE = -1
Public Const DDL_DIRECTORY = &H10
‘ copia tutti i file contenuti in questa dir. Restituisce il numero dei file copiati.
Public 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

‘ Individua il primo file.
search_handle = FindFirstFile( _
from_dir & “*.*”, file_data)
If search_handle INVALID_HANDLE_VALUE Then
‘ Individua gli altri file.
Do
‘ Individua il nome del file.
fname = file_data.cFileName
fname = Left$(fname, InStr(fname, Chr$(0)) – 1)

‘ salta i file denominati “.” and “..”.
If fname “.” And fname “..” Then
files_copied = files_copied + 1

‘ verifica se il file corrente è una directory.
If file_data.dwFileAttributes And DDL_DIRECTORY Then
‘ E’ una directory quindi la crea.
MkDir to_dir & fname

‘ Salva il nome della directory
dirs.Add fname
Else
‘ Non è una directory quindi copia il file.
FileCopy from_dir & fname, to_dir & fname
End If
End If

‘ Individua il file seguente.
If FindNextFile(search_handle, file_data) = 0 Then Exit Do
Loop

FindClose search_handle
End If

‘ Cerca le sottocartelle.
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
‘ Copia da file a file.
‘ La funziona copia tutti i file cartelle e sottocartelle di una cartella specificata.
Public Function XCopyFile(ByVal from_file As String, ByVal to_file As String) As Long
Dim files_copied As Long

‘ E’ una directory?
If GetAttr(from_file) And vbDirectory Then
‘ Sì, è una directory.
If Right$(from_file, 1) “\” Then from_file = from_file & “\”
If Right$(to_file, 1) “\” Then to_file = to_file & “\”

On Error Resume Next
MkDir to_file
If Err.Number = 0 Then files_copied = 1
On Error GoTo 0

files_copied = files_copied + CopyFiles(from_file, to_file)
Else
‘ Non è una directory.
FileCopy from_file, to_file

files_copied = 1
End If

XCopyFile = files_copied
End Function

Ti consigliamo anche

Link copiato negli appunti