Results 1 to 4 of 4

Thread: count only newer files in directory

  1. #1

    Thread Starter
    New Member
    Join Date
    Oct 2020
    Posts
    11

    count only newer files in directory

    i want to create a form where it counts only newer files placed in a specific directory, for example there are 5 files placed , but these files will be deleted, but there can be placed new files also. so general i want to check how may files a day is processed in a certain map

    is this possible without to much code.

    i know i can create an array, but this does not work as it should be..

    Dim result As List(Of String) = (From s1 As String In Me.ListBox2.Items Where Not Me.ListBox1.Items.Contains(s1) Select s1).ToList()
    For Each l In result

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,297

    Re: count only newer files in directory

    You can get all the files in a folder and then filter by creation date/time. You have to have a value to filter by though. There's no magical way to get a "newer" file without knowing what it has to be newer than. As you have provided us no information to go on in that department, I'll leave it up to you to work out how to get that value. Once you have it though, this will get you all files created after that time:
    vb.net Code:
    1. Dim folder As New DirectoryInfo(folderPath)
    2. Dim files = folder.EnumerateFiles.Where(Function(fi) fi.CreationTime > thresholdTime).ToArray()
    files is then an array of FileInfo objects and you can use them as you wish.

  3. #3

    Thread Starter
    New Member
    Join Date
    Oct 2020
    Posts
    11

    Re: count only newer files in directory

    Quote Originally Posted by jmcilhinney View Post
    You can get all the files in a folder and then filter by creation date/time. You have to have a value to filter by though. There's no magical way to get a "newer" file without knowing what it has to be newer than. As you have provided us no information to go on in that department, I'll leave it up to you to work out how to get that value. Once you have it though, this will get you all files created after that time:
    vb.net Code:
    1. Dim folder As New DirectoryInfo(folderPath)
    2. Dim files = folder.EnumerateFiles.Where(Function(fi) fi.CreationTime > thresholdTime).ToArray()
    files is then an array of FileInfo objects and you can use them as you wish.
    thx i will give it a try

  4. #4

    Thread Starter
    New Member
    Join Date
    Oct 2020
    Posts
    11

    Re: count only newer files in directory

    after testing i finaly ended with some other code, wich works in my situation, thx anyway

    Code:
    Imports System.IO
    Imports System
    Imports System.Diagnostics
    Imports System.Threading
    
    Public Class Form1
    
        Public watchfolder As FileSystemWatcher
        Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
            watchfolder = New System.IO.FileSystemWatcher()
            watchfolder.Path = "d:\jb\"
            watchfolder.NotifyFilter = IO.NotifyFilters.DirectoryName
            watchfolder.NotifyFilter = watchfolder.NotifyFilter Or IO.NotifyFilters.FileName
            watchfolder.NotifyFilter = watchfolder.NotifyFilter Or IO.NotifyFilters.Attributes
            AddHandler watchfolder.Created, AddressOf logchange
    
            watchfolder.EnableRaisingEvents = True
    
        End Sub
    
        Private Sub logchange(ByVal source As Object, ByVal e As System.IO.FileSystemEventArgs)
    
            If e.ChangeType = IO.WatcherChangeTypes.Created Then
                GlobalVariables.teller2 = GlobalVariables.teller2 + 1
                Invoke(New SetTextDel(AddressOf SetText), "File ")
            End If
    
        End Sub
    
        Private Delegate Sub SetTextDel(ByVal msg As String)
    
        Private Sub SetText(ByVal msg As String)
    
            TextBox1.Text = GlobalVariables.teller2
    
        End Sub
    
        Public Class GlobalVariables
            Public Shared Property teller2 As Integer
    
        End Class
    
    End Class

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width