1

使用以下代码,我试图删除闪存驱动器上文件夹中的特定文件,然后将剩余文件复制到单独的文件夹中。当程序运行并且我启动按钮执行此操作时,程序会删除过去一年内未修改的文件,但随后它不会继续提取剩余的文件并将它们放入单独的文件夹中。

有谁知道为什么?

Imports System.IO

Public Class frmExtractionator

    Dim txtFiles1 As Control

    Private Sub btnStart_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnStart.Click

        Dim sourceDirectory As String = "E:\CopierFolderforTestDriveCapstone"
        Dim archiveDirectory As String = "E:\FilesExtracted"

        Try
            DeleteUnmodifiedFiles(sourceDirectory, 365)

            Dim txtFiles = Directory.EnumerateFiles(sourceDirectory)

            If (Not System.IO.Directory.Exists(archiveDirectory)) Then
                System.IO.Directory.CreateDirectory(archiveDirectory)
            End If

            For Each currentFileLoc As String In txtFiles
                Dim fileName = currentFileLoc.Substring(sourceDirectory.Length + 1)
                File.Move(currentFileLoc, Path.Combine(archiveDirectory, fileName))
            Next
        Catch eT As Exception
            Console.WriteLine(eT.Message)
        End Try
    End Sub

    Private Sub DeleteUnmodifiedFiles(ByVal directoryName As String, ByVal modificationThresholdDays As Integer)
        Dim folder As New DirectoryInfo(directoryName)
        Dim thresholdDate As Date
        Dim wasModifiedSinceThreshold As Boolean
        For Each file As FileInfo In folder.GetFiles
            thresholdDate = DateTime.Now().AddDays(-1 * modificationThresholdDays)
            wasModifiedSinceThreshold = (file.LastWriteTime > thresholdDate)
            If (Not wasModifiedSinceThreshold) Then file.Delete()
        Next
        MessageBox.Show("Deleting Files")
    End Sub
End Class
4

1 回答 1

1

这将删除源目录中一年未修改的所有文件,然后将所有剩余文件移动到目标目录...

Private Sub btnStart_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnStart.Click
    Dim fileListA() As String
    fileListA = (IO.Directory.GetFiles("C:\Scource_Directory"))
    For Each i As String In fileListA
        If (IO.File.GetLastWriteTime(i).ToShortDateString.Substring(6)) < (CType(DateTime.Now.Year.ToString, Integer) - 1) Then
            IO.File.Delete(i)
        End If
    Next
    Dim fileListB() As String
    fileListB = (IO.Directory.GetFiles("C:\Scource_Directory"))
    For Each i As String In fileListB
        IO.File.Move(i, "Destination_Directory")
    Next
End Sub
于 2013-04-11T11:31:57.483 回答