| 1 | #Region "Microsoft.VisualBasic::b62bde2bf71d64669151efc2987e6471, Microsoft.VisualBasic.Core\Extensions\IO\Path\CopyDirectory.vb" |
| 2 | |
| 3 | ' Author: |
| 4 | ' |
| 5 | ' asuka (amethyst.asuka@gcmodeller.org) |
| 6 | ' xie (genetics@smrucc.org) |
| 7 | ' xieguigang (xie.guigang@live.com) |
| 8 | ' |
| 9 | ' Copyright (c) 2018 GPL3 Licensed |
| 10 | ' |
| 11 | ' |
| 12 | ' GNU GENERAL PUBLIC LICENSE (GPL3) |
| 13 | ' |
| 14 | ' |
| 15 | ' This program is free software: you can redistribute it and/or modify |
| 16 | ' it under the terms of the GNU General Public License as published by |
| 17 | ' the Free Software Foundation, either version 3 of the License, or |
| 18 | ' (at your option) any later version. |
| 19 | ' |
| 20 | ' This program is distributed in the hope that it will be useful, |
| 21 | ' but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 22 | ' MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 23 | ' GNU General Public License for more details. |
| 24 | ' |
| 25 | ' You should have received a copy of the GNU General Public License |
| 26 | ' along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 27 | |
| 28 | |
| 29 | |
| 30 | ' /********************************************************************************/ |
| 31 | |
| 32 | ' Summaries: |
| 33 | |
| 34 | ' Class CopyDirectoryAction |
| 35 | ' |
| 36 | ' Constructor: (+1 Overloads) Sub New |
| 37 | ' |
| 38 | ' Function: CreateDestinationFolderAndReturnNewPath |
| 39 | ' |
| 40 | ' Sub: Copy, CopyFilesToTargetDirectory, CopySubDirectoriesWithFiles |
| 41 | ' |
| 42 | ' |
| 43 | ' /********************************************************************************/ |
| 44 | |
| 45 | #End Region |
| 46 | |
| 47 | Imports System.IO |
| 48 | Imports Microsoft.VisualBasic.Language.UnixBash |
| 49 | |
| 50 | Namespace FileIO |
| 51 | |
| 52 | Public Class CopyDirectoryAction |
| 53 | |
| 54 | ReadOnly progress As IProgress(Of String) |
| 55 | |
| 56 | Sub New(progress As Progress(Of String)) |
| 57 | Me.progress = progress |
| 58 | End Sub |
| 59 | |
| 60 | ''' <summary> |
| 61 | ''' 这个函数会在这个模块内被递归调用 |
| 62 | ''' </summary> |
| 63 | ''' <param name="src$"></param> |
| 64 | ''' <param name="destination$"></param> |
| 65 | Public Sub Copy(src$, destination$, Optional includeSrc As Boolean = True) |
| 66 | Dim directory As New DirectoryInfo(src) |
| 67 | |
| 68 | If includeSrc Then |
| 69 | If FileIO.Directory.Exists(Path.Combine(destination, directory.Name)) Then |
| 70 | Call $"Directory '{directory.Name}' already exists in '{destination}'".Warning |
| 71 | End If |
| 72 | |
| 73 | destination = CreateDestinationFolderAndReturnNewPath(src, destination) |
| 74 | Else |
| 75 | If FileIO.Directory.Exists(destination) Then |
| 76 | Call $"Directory '{destination.DirectoryName}' already exists in '{destination}'".Warning |
| 77 | End If |
| 78 | End If |
| 79 | |
| 80 | Call CopyFilesToTargetDirectory(src, destination) |
| 81 | Call CopySubDirectoriesWithFiles(src, destination) |
| 82 | End Sub |
| 83 | |
| 84 | Private Shared Function CreateDestinationFolderAndReturnNewPath(src As String, destinationFolder$) As String |
| 85 | Dim directory As New DirectoryInfo(src) |
| 86 | Dim path = IO.Path.Combine(destinationFolder, directory.Name) |
| 87 | Call path.MkDIR |
| 88 | Return path |
| 89 | End Function |
| 90 | |
| 91 | Private Sub CopyFilesToTargetDirectory(src As String, destinationFolder$) |
| 92 | Dim resultFilePath$ |
| 93 | |
| 94 | ' 2017-12-24 |
| 95 | ' 因为只是复制当前的文件夹的文件,所以在这里就不需要添加-r递归参数了 |
| 96 | For Each path As String In ls - l - "*.*" <= src |
| 97 | With New FileInfo(path) |
| 98 | resultFilePath = IO.Path.Combine(destinationFolder, IO.Path.GetFileName(.Name)) |
| 99 | End With |
| 100 | |
| 101 | Call progress.Report(resultFilePath) |
| 102 | Call path.FileCopy(resultFilePath) |
| 103 | Next |
| 104 | End Sub |
| 105 | |
| 106 | Private Sub CopySubDirectoriesWithFiles(pathToSourceFolder As String, pathToDestinationFolder As String) |
| 107 | For Each subDirectory As String In IO.Directory.GetDirectories(pathToSourceFolder) |
| 108 | Copy(subDirectory, pathToDestinationFolder, includeSrc:=True) |
| 109 | Next |
| 110 | End Sub |
| 111 | End Class |
| 112 | End Namespace |