Copy the whole directory

zhaozj2021-02-16  87

'Usage:' Copy Recursive with Overwrite if exists. 'RecursiveDirectoryCopy ( "C: / Data", "D: / Data", True, True)' Copy Recursive without Overwriting 'RecursiveDirectoryCopy (. "C: / Data", "D : / Data ", True, False) 'Copy this directory only Overwrite if exists..' RecursiveDirectoryCopy (" C: / Data "," D: / Data ", False, True) 'Copy this directory only without overwriting.' RecursiveDirectoryCopy ( "C: / Data", "D: / Data", False, False) 'Recursively copy all files and subdirectories from the specified source to the specified' destination Private Sub RecursiveDirectoryCopy (ByVal sourceDir As String, ByVal destDir As String,. ByVal fRecursive As Boolean, ByVal overWrite As Boolean) Dim sDir As String Dim dDirInfo As IO.DirectoryInfo Dim sDirInfo As IO.DirectoryInfo Dim sFile As String Dim sFileInfo As IO.FileInfo Dim dFileInfo As IO.FileInfo 'Add trailing separators to the supplied paths if they don't exist. if not sourcedir.endswith (System.io.path.directoryseParetor Char.ToString ()) Then sourceDir & = System.IO.Path.DirectorySeparatorChar End If If Not destDir.EndsWith (System.IO.Path.DirectorySeparatorChar.ToString ()) Then destDir & = System.IO.Path.DirectorySeparatorChar End If 'If destination directory does not exist, create it. dDirInfo = New System.IO.DirectoryInfo (destDir) If dDirInfo.Exists = False Then dDirInfo.Create () dDirInfo = Nothing' Recursive switch to continue drilling down into directory structure. If fRecursive THEN '

Get a list of directories from the current parent. For Each sDir In System.IO.Directory.GetDirectories (sourceDir) sDirInfo = New System.IO.DirectoryInfo (sDir) dDirInfo = New System.IO.DirectoryInfo (destDir & sDirInfo.Name) 'Create the directory if it does not exist. If dDirInfo.Exists = False Then dDirInfo.Create ()' Since we are in recursive mode, copy the children also RecursiveDirectoryCopy (sDirInfo.FullName, dDirInfo.FullName, fRecursive, overWrite) sDirInfo = Nothing dDirInfo = Nothing Next End If 'Get the files from the current parent. For Each sFile In System.IO.Directory.GetFiles (sourceDir) sFileInfo = New System.IO.FileInfo (sFile) dFileInfo = New System.IO.FileInfo ( Replace (SFile, SourceDir, destdir) 'if file does not exists = false damfo.exists = false damfo.copyto (dfileinfo.fullname, overwrite) Else 'If file exists and is the same length (size). Skip.' If file exists and is of different Length (size) and overwrite = True. Copy If sFileInfo.Length <> dFileInfo.Length AndAlso overWrite Then sFileInfo.CopyTo ( dFileInfo.FullName, overWrite) 'If file exists and is of different Length (size) and overwrite = False. Skip ElseIf sFileInfo.Length <> dFileInfo.Length AndAlso Not overWrite Then Debug.WriteLine (sFileInfo.FullName & "Not copied." ) End if end if sfileinfo = Nothing dfileinfo =

Nothing Next End Sub 'calls RecursiveDirectoryCopy ("C: / Test1", "D: / Test1", True, True)

转载请注明原文地址:https://www.9cbs.com/read-13404.html

New Post(0)