如何在visual basic.net中搜索文件夹并复制文件夹?

时间:2011-12-13 08:38:30

标签: vb.net directory

我找不到如何搜索包含字符串的文件夹(目录),并将包含该字符串的所有文件夹(目录)复制到另一个目录。因此,您搜索目录的名称,而不是目录中的文件。 例如:E:\中的'KTNPRK'给出: E:\ KTNPRK1,E:\ AZNKTNPR76等......

1 个答案:

答案 0 :(得分:1)

以下是如何移动目录的示例:

    Dim sSourcePath As String
    Dim sDestPath As String
    Dim sTextToFind As String

    sSourcePath = "D:\Temp"
    sDestPath = "D:\Temp1"
    sTextToFind = "test"

    For Each sDir In Directory.GetDirectories(sSourcePath, "*" & sTextToFind & "*")
        Directory.Move(sDir, Path.Combine(sDestPath, Path.GetFileName(sDir)))
    Next

为了复制文件夹中的所有文件,可以将循环更改为:

        Dim sFullDestDir As String

        sFullDestDir = Path.Combine(sDestPath, IO.Path.GetFileName(sFullSourceDir))

        ' Create the directory if it doesn't exist
        If Not Directory.Exists(sFullDestDir) Then
            Directory.CreateDirectory(sFullDestDir)
        End If

        ' Copy the files in the directory
        ' If subfolders are to be copied, this logic can be turned into a recursive method.
        For Each sFileName As String In Directory.GetFiles(sFullSourceDir)
            File.Copy(sFileName, Path.Combine(sFullDestDir, IO.Path.GetFileName(sFileName)))
        Next