使用vb.net递归读取文件夹中的文件

时间:2019-02-21 10:44:57

标签: vb.net

我写了一个程序,它不检查文件更新时间,但不检查递归文件夹文件。也为递归文件夹文件提供帮助。 我的代码在这里:

子getfilestat1()     昏暗的fileName作为字符串     昏暗的CurrCyleTime作为日期     Dim PrevCycleTime作为日期     昏暗的DBCycleTime作为日期

Dim connectionString As String, sql As String
Dim _SQLConnection As AseConnection
Dim _SQLCommand As AseCommand
Dim _SQLAdapter As AseDataAdapter
Dim _DataSet As DataSet
Dim _SQLReader As AseDataReader


_SQLConnection = New AseConnection
_SQLCommand = New AseCommand
_SQLConnection.ConnectionString = "Data Source='10.49.196.97';Port=9713;Database=db_print;Uid=kuat199;Pwd=testing1; "
_SQLCommand.Connection = _SQLConnection
_SQLCommand.CommandText = ""
_SQLCommand.CommandType = CommandType.Text
_SQLCommand.CommandTimeout = 900000000


_SQLConnection.Open()


Dim command As New AseCommand("select * from Kampachi_Cycle", _SQLConnection)
Dim reader As AseDataReader = command.ExecuteReader()
While reader.Read()

    ' Console.WriteLine(reader("pol_no").ToString() & " " & Convert.ToString(reader("image_return")) & " " & Convert.ToString(reader("no_of_images")))
    DBCycleTime = reader("CYCLE").ToString()
End While
' Dim asSettings As AppSettingsSection = cAppConfig.AppSettings

'Dim fi As New System.IO.DirectoryInfo("D:\Vimal\test")
Dim fi As New System.IO.DirectoryInfo("\\kaip3r7ciwf01\BicorData\report\kam\")
Dim files = fi.GetFiles("*", SearchOption.AllDirectories).ToList()


'For Each filename As String In IO.Directory.GetFiles(Directory, "*", IO.SearchOption.AllDirectories)
'For Each file In files Select file Order By file.CreationTime Descending
''Dim first = (From file In files Select file Order By file.CreationTime Ascending).FirstOrDefault



'Count the number files in network path
Dim fcount = files.Count()

'Fetching the previous cycle run time from config  file
PrevCycleTime = ConfigurationManager.AppSettings("PrevCycleTime")
CurrCyleTime = Now()
ConfigurationManager.AppSettings("PrevCycleTime") = CurrCyleTime

''''My.Settings.Save()
For i As Integer = 0 To fcount - 1
    If files(i).LastWriteTime > DBCycleTime.AddMinutes(-20) Then
        fileName = files(i).Name.ToString()
        Dim insertCmd As New AseCommand("INSERT INTO Kampachi_FilesProcess " + " ( FILENAME, FileReadStatus) " + " VALUES( @file_name, @read_stat )", _SQLConnection)
        Dim parm As New AseParameter("@file_name", AseDbType.VarChar, 1000)
        insertCmd.Parameters.Add(parm)
        parm = New AseParameter("@read_stat", AseDbType.VarChar, 12)
        insertCmd.Parameters.Add(parm)
        Dim recordsAffected As Integer
        insertCmd.Parameters(0).Value = fileName
        insertCmd.Parameters(1).Value = "Y"
        recordsAffected = insertCmd.ExecuteNonQuery()

        If i = 0 Then
            fileName = files(i).Name.ToString()
            Dim updCmd As New AseCommand("update Kampachi_Cycle set CYCLE = Getdate()", _SQLConnection)
            Dim updparm As New AseParameter("@file_name", AseDbType.VarChar, 1000)
            recordsAffected = updCmd.ExecuteNonQuery()

        End If


    End If


Next

结束子

完成这些更改后,它看起来还不错,并且可以正确分配。 它也提供递归阅读。

1 个答案:

答案 0 :(得分:1)

更改此行:

Dim files = fi.GetFileSystemInfos.ToList()

收件人:

Dim files = fi.GetFiles("*", SearchOption.AllDirectories).ToList()

要回答关于If而不检查所有文件的以下问题:您是正确的,但是您的代码明确使用了FirstOrDefault方法,因此它只会检查第一个文件。我不知道您在这里对程序的其余部分正在做什么,并且您的问题未指定,但是以上内容回答了您有关递归文件搜索的问题。

要获取所有早于25分钟的文件的列表,请使用以下代码:

Dim files As List(Of FileInfo) = fi.GetFiles("*", SearchOption.AllDirectories).ToList
Dim oldFileTimeStamp As DateTime = DateTime.Now.AddMinutes(-25)
Dim olderFiles As List(Of FileInfo) = files.Where(Function(fi2) fi2.LastWriteTime > oldFileTimeStamp).ToList()

请,如果它回答了这个特定问题,请单击接受的答案按钮。如果您还有其他与原始问题无关的问题,请打开一个新的Stackoverflow问题,不要将新问题添加到现有的Stackoverflow问题中。这样一来,以后的观众就可以更轻松地找到您后续问题的答案(即:搜索不会在问题内找到问题,只会找到原始问题)。