0
votes

I wrote a program which is not checking the file update time but its not checking recursive folder files.kindly help for recursive folder files as well. My code is here :

Sub getfilestat1() Dim fileName As String Dim CurrCyleTime As Date Dim PrevCycleTime As Date Dim DBCycleTime As Date

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

End Sub

After these changes it looks fine and giving out properly. It is giving recursive reading as well.

1
What part of your code do you think should be recursive?Enigmativity
@Enigmativity part which read the files inside folder.it cannt read subfolders files. Dim files = fi.GetFileSystemInfos.ToListvim

1 Answers

1
votes

Change this line:

Dim files = fi.GetFileSystemInfos.ToList()

To:

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

To answer below question about the If not checking all of the files: You are correct, but your code explicitly used the FirstOrDefault method so it would only ever examine the first file. I don't know what you're doing with the rest of your program here, and your question didn't specify, but the above answered your question about recursive file searching.

To get a list of all the files that are older than 25 minutes use this code:

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()

Please, if this answered this specific question, please click the accepted answer button. If you have additional questions, unrelated to the original question, please open a new Stackoverflow question, and do not add new questions to an existing Stackoverflow question. This makes it easier for future viewers to find answers to your follow up question(s) (ie: search won't find questions inside of question, it only finds the original question).