0

我在本月 30 日有一项任务要做,需要我手动浏览目录中的数千个文件夹,并删除在某个日期创建的文件夹中的所有文件夹和文件。

我想我可以编写一个快速程序来为我处理这个问题。我有一个带有 UNC 路径文本框的 winform、一个用于传递日期的日期时间选择器和一个用于删除文件的按钮。到目前为止,我只有这个,我想确保我走在正确的道路上。

Imports System.IO

Public Class FormMain

    Private Sub btn_DeleteFolders_Click(sender As Object, e As EventArgs) Handles btn_DeleteFolders.Click
        Dim UNC As String = tb_UNC.Text
        Dim FDate As String = dt_FolderDate.Value.Date
        Dim FPath As New System.IO.DirectoryInfo(UNC)
        Dim CreatedDate As String = way to find the created date of a folder??
        'MessageBox.Show(FPath)

        For Each f As String In Directory.EnumerateFiles(UNC, FDate, SearchOption.AllDirectories)
            If FDate = CreatedDate Then

                File.Delete(UNC)

            End If
        Next

    End Sub
End Class

任何帮助都是极好的!!!谢谢!

4

3 回答 3

1

该类FileInfo具有文件的创建日期,因此请尝试以下操作:

Dim compareToDate As DateTime = CType(dt_FolderDate.Value.Date, DateTime)
Dim files As String() = Directory.EnumerateFiles(dirName)

For Each file As String In files
    ' Create object to hold file information
    Dim fi As New FileInfo(file)

    ' Compare the creation date to the date selected in the date picker
    If fi.CreationTime.Date = compareToDate.Date Then
        ' The dates match, so delete the file
        fi.Delete()
    End If
Next
于 2013-09-20T18:05:28.557 回答
0

FileInfo 类包含一个可以告诉您这一点的属性。这是 C# 中的代码,但它应该很容易转换为 VB。

using System.IO;

private DateTime GetCreationdDate(string FolderPath)
{
    FileInfo FI = new FileInfo(FolderPath);
    return FI.CreationTime;
}
于 2013-09-20T18:04:42.187 回答
0

这个怎么样:

Private Sub Button8_Click(sender As System.Object, e As System.EventArgs) Handles Button8.Click
    Dim sPath As String = "P:\ToDropBox"
    Dim dtCreateDate As Date
    For Each SFolder As String In IO.Directory.GetDirectories(sPath)
        dtCreateDate = IO.Directory.GetCreationTime(SFolder)
        If dtCreateDate.ToShortDateString = Now.ToShortDateString Then
            Process.Start("cmd.exe", "/C rd /s " & SFolder)
        End If
    Next
End Sub

示例删除了“今天”创建的文件夹,因此您不希望这样做......我会进行一些日期限制检查以确保。

DOS 命令提示输入 ay/n,我认为如果你勇敢的话,有一个开关可以抑制它。如果路径有空格,则需要将路径用双引号引起来。 http://ss64.com/nt/rd.html

于 2013-09-20T19:45:52.413 回答