从目标路径中删除子文件夹

时间:2014-02-28 11:12:35

标签: vb.net subdirectory

我的文件夹结构如下:

Source path --->Folder
                  --> SubFolder
                      (Ex: MyFolder)

Destination path --->MyFolder
                  --> SubFolder and zip File with sub Folder name
                      (Ex: Feb 28 and Feb28.zip)

现在我需要通过保留zip文件来删除子文件夹。

如何删除它们?

这是我的代码:

Dim sourceDirectoryInfo As New System.IO.DirectoryInfo(sourcePath)

        ' If the destination folder don't exist then create it
        If Not System.IO.Directory.Exists(destinationPath) Then
            System.IO.Directory.CreateDirectory(destinationPath)
        End If

        Dim fileSystemInfo As System.IO.FileSystemInfo
        For Each fileSystemInfo In sourceDirectoryInfo.GetFileSystemInfos
            Dim destinationFileName As String = System.IO.Path.Combine(destinationPath, fileSystemInfo.Name)

            ' Now check whether its a file or a folder and take action accordingly
            If TypeOf fileSystemInfo Is System.IO.FileInfo Then
                If Not System.IO.Directory.Exists(destinationPath) Then
                    System.IO.Directory.CreateDirectory(destinationPath)
                End If
                'System.IO.File.Copy(fileSystemInfo.FullName, destinationFileName, True)
                Dim Files As FileInfo() = (From [File] As FileInfo In Source.GetFiles("*", SearchOption.AllDirectories) Where [File].LastWriteTime.Date <= (Date.Today.AddDays(-2))).ToArray

                For Each [File] As FileInfo In Files
                    IO.File.Copy([File].FullName,
                                 Path.Combine(Target.FullName, [File].Name), True)
                Next [File]
            Else

                ' Recursively call the mothod to copy all the neste folders
                'CopyDirectory(fileSystemInfo.FullName, destinationFileName)
                Dim Files As FileInfo() =
           (From [File] As FileInfo In Source.GetFiles("*", SearchOption.AllDirectories)
            Where [File].LastWriteTime.Date <= (Date.Today.AddDays(-2))).ToArray

                For Each [File] As FileInfo In Files
                    final = File.Directory.Name
                    DeletePath = Target.FullName & "\" & final & "\"
                    destinationPath = Target.FullName & "\" & final & "\" & simpledate & " " & simplemonth
                    myfilename = final
                    If Not System.IO.Directory.Exists(Target.FullName & "\" & final & "\" & simpledate & " " & simplemonth) Then
                        System.IO.Directory.CreateDirectory(Target.FullName & "\" & final & "\" & simpledate & " " & simplemonth)
                    End If
                    IO.File.Copy([File].FullName,
                             Path.Combine(destinationPath, [File].Name), True)
                    Using zip As ZipFile = New ZipFile
                        zip.AddDirectory(destinationPath)
                        zip.Save(destinationPath & ".zip")
                    End Using
                Next [File]
            End If
            For Each dir As String In Directory.GetDirectories(DeletePath)
                DeleteDirectory(dir)
            Next
        Next
End Sub

0 个答案:

没有答案