Results 1 to 20 of 20

Thread: [RESOLVED] Get files from Folder using timer event

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Sep 2006
    Posts
    352

    Resolved [RESOLVED] Get files from Folder using timer event

    Hi I am a newbie in VB.net. I would like to get the filename and path of txt files one at a time in a Folder after the timer event. Lets say I have a folder named C:\Test with text files inside, how can I get the file name and path then execute the timer for a period before getting the next file till all the files in the folder have been read ? Thanks

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

    Re: Get files from Folder using timer event

    You're looking at Timers incorrectly. You don't execute a Timer for a period. The Timer is either enabled or disabled and, if it's enabled, it will raise a Tick event at regular intervals, that being determined by the Interval property.

    Also, your question doesn't really make sense as it's asked. Do you really mean that you want to get a list of files in a folder and then, on each Tick event, process one of those files in some way? To simply get a file path on each Tick as you have literally asked for doesn't really seem useful. I'm going to assume that you actually do mean to get a list of file paths initially and then process them one by one and suggest the following:

    1. Create a Directory object for your folder and call its GetFiles method to get the list of file paths, putting that list into a Queue(Of String).
    2. Call the Start method of your Timer.
    3. On the Tick event of the Timer, call the Dequeue method of the queue to get the next file path and process it appropriately.
    4. When the last item is dequeued, call the Stop method of the Timer.

    With regards to step 3, note that processing the file in the Tick event handler or a method called from the Tick event handler will mean that the processing occurs on the UI thread. That will freeze your UI and prevent any further Tick events being raised until the work completes. If the work takes any sort of significant time then it should be done on a secondary thread.

  3. #3

    Thread Starter
    Hyperactive Member
    Join Date
    Sep 2006
    Posts
    352

    Re: Get files from Folder using timer event

    Hi and thanks for your answer. Here is the code that I started work
    Private zMailbox As String = "c:\Fold\"

    Code:
    Dim files As FileInfo() Dim index As Integer = 0
    
    Private Sub Button1_Click(ByVal sender As Object, ByVal e As EventArgs) Handles Button1.Click
        Dim finfo As New IO.DirectoryInfo(zMailbox)
        files = finfo.GetFiles("*.txt")
        Timer1.Start()
    End Sub
    
    Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
        If index >= files.Length Then
            index = 0
        End If
        TextBox1.Text = (ListBox1.Items.Add(files(index)))
    
        index += 1
    However Im getting trouble in converting the
    Code:
    (ListBox1.Items.Add(files(index)))
    into a string as I want to use the path it produces after the timer tick event elsewhere. Thanks

    Quote Originally Posted by jmcilhinney View Post
    You're looking at Timers incorrectly. You don't execute a Timer for a period. The Timer is either enabled or disabled and, if it's enabled, it will raise a Tick event at regular intervals, that being determined by the Interval property.

    Also, your question doesn't really make sense as it's asked. Do you really mean that you want to get a list of files in a folder and then, on each Tick event, process one of those files in some way? To simply get a file path on each Tick as you have literally asked for doesn't really seem useful. I'm going to assume that you actually do mean to get a list of file paths initially and then process them one by one and suggest the following:

    1. Create a Directory object for your folder and call its GetFiles method to get the list of file paths, putting that list into a Queue(Of String).
    2. Call the Start method of your Timer.
    3. On the Tick event of the Timer, call the Dequeue method of the queue to get the next file path and process it appropriately.
    4. When the last item is dequeued, call the Stop method of the Timer.

    With regards to step 3, note that processing the file in the Tick event handler or a method called from the Tick event handler will mean that the processing occurs on the UI thread. That will freeze your UI and prevent any further Tick events being raised until the work completes. If the work takes any sort of significant time then it should be done on a secondary thread.

  4. #4
    Smooth Moperator techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,532

    Re: Get files from Folder using timer event

    well files is an array of FileInfo... so you just need to access the appropriate property of it when adding it to the list box.

    However, your code doesn't make sense...
    1) why not just add the file list? What is the point of the timer?
    2) once it hits the end of the list, it's going to just loop around, repeating everything again
    3) and again and again and again, since you never turn off the timer.
    4) What do you expect to be in Textbox1? The Listbox.Items.Add method is that... a method... it isn't a function, so it's not going to return anything. So I'm not sure what you expect that line to return.

    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  5. #5

    Thread Starter
    Hyperactive Member
    Join Date
    Sep 2006
    Posts
    352

    Re: Get files from Folder using timer event

    I thought as much that the code looks awe full. Let me try and explain what what I am after. in an example.
    1. I have a Folder C\Test with txt files.
    2. Get path of first text file.
    3. wait 5 mins
    4. Get path of second text file
    5. wait 5 mins
    6. get path of third file
    7. wait 5 mins
    8. do same for all files in folder
    9. stop !


    I hope its more explicit now. Thanks

  6. #6
    Smooth Moperator techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,532

    Re: Get files from Folder using timer event

    Still doesn't make sense... why the wait?
    What if files get added in the meantime?

    At any rate if you want it to stop, then you need to tell it to stop:
    Code:
        If index >= files.Length Then
            index = 0
        End If
    should be
    Code:
        If index >= files.Length Then
            timer1.enabled = false 'stop the timer to stop processing
        End If
    as for getting the path... try looking up the FileInfo class... it's there... seek and ye shall find.

    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  7. #7
    PowerPoster SJWhiteley's Avatar
    Join Date
    Feb 2009
    Location
    South of the Mason-Dixon Line
    Posts
    2,256

    Re: Get files from Folder using timer event

    As an additional note/question, what is the relationship which orders the files? there is no 'first file' - generically - in a folder.
    "Ok, my response to that is pending a Google search" - Bucky Katt.
    "There are two types of people in the world: Those who can extrapolate from incomplete data sets." - Unk.
    "Before you can 'think outside the box' you need to understand where the box is."

  8. #8

    Thread Starter
    Hyperactive Member
    Join Date
    Sep 2006
    Posts
    352

    Re: Get files from Folder using timer event

    Nope no sequential order at the moment. All I would like to do is get their paths in a string at given intervals . Stop the loop after I have got the last file. Thats all. Thanks

  9. #9
    Smooth Moperator techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,532

    Re: Get files from Folder using timer event

    you already had the wood and nails... you've been given the tools (how to stop the loop and what to go lookup)... time now to go build the shelves.

    In fact here, have a hammer:
    http://msdn.microsoft.com/en-us/libr...vs.110%29.aspx

    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  10. #10

    Thread Starter
    Hyperactive Member
    Join Date
    Sep 2006
    Posts
    352

    Re: Get files from Folder using timer event

    Many thanks while praying that I might not miss the nail with the hammer and hit my finger
    Will let you know . Thanks again
    Quote Originally Posted by techgnome View Post
    you already had the wood and nails... you've been given the tools (how to stop the loop and what to go lookup)... time now to go build the shelves.

    In fact here, have a hammer:
    http://msdn.microsoft.com/en-us/libr...vs.110%29.aspx

    -tg

  11. #11
    Smooth Moperator techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,532

    Re: Get files from Folder using timer event

    Don't worry, we have plenty of band-aids around here to help when needed.

    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  12. #12

    Thread Starter
    Hyperactive Member
    Join Date
    Sep 2006
    Posts
    352

    Re: Get files from Folder using timer event

    Quote Originally Posted by techgnome View Post
    Don't worry, we have plenty of band-aids around here to help when needed.

    -tg
    I succeeded in getting the string but its value doesn't change after the timer. This one skips right to the last after the timer interval after that no change.

    Code:
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    
            Timer1.Start()
    
        End Sub
    
        Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
            Dim strFileSize As String = ""
            Dim di As New IO.DirectoryInfo("C:\Blog1")
            Dim aryFi As IO.FileInfo() = di.GetFiles("*.txt")
            Dim fi As IO.FileInfo
            For Each fi In aryFi
                TextBox1.Text = ""
                TextBox1.Text = fi.FullName
    
    
            Next
        End Sub
    End Class

  13. #13
    Smooth Moperator techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,532

    Re: Get files from Folder using timer event

    that's because in the timer, you get a list of files, loop through each one, displaying it... so by the time the loop is done, yeah, it's only going to display the last thing in the list...

    You were closer with your original code... get a list of the files... then in the loop display the next one...

    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  14. #14

    Thread Starter
    Hyperactive Member
    Join Date
    Sep 2006
    Posts
    352

    Re: Get files from Folder using timer event

    Quote Originally Posted by techgnome View Post
    that's because in the timer, you get a list of files, loop through each one, displaying it... so by the time the loop is done, yeah, it's only going to display the last thing in the list...

    You were closer with your original code... get a list of the files... then in the loop display the next one...

    -tg
    Okay Hopping off to where I started. I hope I dont end up in a loop

  15. #15

    Thread Starter
    Hyperactive Member
    Join Date
    Sep 2006
    Posts
    352

    Re: Get files from Folder using timer event

    Quote Originally Posted by tendemo View Post
    Okay Hopping off to where I started. I hope I dont end up in a loop
    Back to where I started off

    Code:
    Private zMailbox As String = "c:\Blog1\"
        Dim files As FileInfo()
        Dim index As Integer = 0
    
        Private Sub Button1_Click(ByVal sender As Object, ByVal e As EventArgs) Handles Button1.Click
            Dim finfo As New IO.DirectoryInfo(zMailbox)
            files = finfo.GetFiles("*.txt")
            Timer1.Start()
        End Sub
    
        Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
            If index >= files.Length Then
                Timer1.Enabled = False 'stop the timer to stop processing
            End If
            TextBox1.Text = files(index) ' get and error here as its impossible to convert to string and cant get path
    
            index += 1
        End Sub
    
    
    End Class
    Seems I got caught in my very own loop.

  16. #16
    Smooth Moperator techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,532

    Re: Get files from Folder using timer event

    yes... FileInfo is a structure... it contains a number of properties... did you click on the hammer I left earlier? Me thinks you didn't... because if you had, you would have seen "FullPath" listed under the properties, which is "the full path of the directory or file."
    http://msdn.microsoft.com/en-us/libr...v=vs.110).aspx

    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  17. #17

    Thread Starter
    Hyperactive Member
    Join Date
    Sep 2006
    Posts
    352

    Re: Get files from Folder using timer event

    I did , each time I tried to put a full path reference my code editor just spat it out.

  18. #18
    Smooth Moperator techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,532

    Re: Get files from Folder using timer event

    "Spat it out?" what does that mean? what EXACTLY did you try? IT should be just this:
    Code:
    TextBox1.Text = files(index).FullPath
    That's it...

    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  19. #19

    Thread Starter
    Hyperactive Member
    Join Date
    Sep 2006
    Posts
    352

    Re: Get files from Folder using timer event

    Quote Originally Posted by techgnome View Post
    "Spat it out?" what does that mean? what EXACTLY did you try? IT should be just this:
    Code:
    TextBox1.Text = files(index).FullPath
    That's it...

    -tg
    Ouch, I can swear I did that and backed off because its gives an error about the
    Code:
    files(index).FullPath
    It says " the system.IO.filesysteminfo.fullpath cant be accessed in this context as its protected" (Hahah did a translation as my editor is in French).
    Here is my full code below
    Code:
    Imports System.IO
    Imports System.IO.FileInfo
    Public Class Form1
        Private zMailbox As String = "c:\Blog1\"
    
        Dim files As FileInfo()
        Dim index As Integer = 0
    
    
        Private Sub Button1_Click(ByVal sender As Object, ByVal e As EventArgs) Handles Button1.Click
            Dim finfo As New IO.DirectoryInfo(zMailbox)
            files = finfo.GetFiles("*.txt")
            Timer1.Start()
        End Sub
    
        Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
            If index >= files.Length Then
                index = 0
            End If
            TextBox1.Text = files(index).FullPath
            index += 1
        End Sub
    End Class

  20. #20

    Thread Starter
    Hyperactive Member
    Join Date
    Sep 2006
    Posts
    352

    Re: Get files from Folder using timer event

    Did an F12 and hit run and all was well, so many thanks again
    Quote Originally Posted by tendemo View Post
    Ouch, I can swear I did that and backed off because its gives an error about the
    Code:
    files(index).FullPath
    It says " the system.IO.filesysteminfo.fullpath cant be accessed in this context as its protected" (Hahah did a translation as my editor is in French).
    Here is my full code below
    Code:
    Imports System.IO
    Imports System.IO.FileInfo
    Public Class Form1
        Private zMailbox As String = "c:\Blog1\"
    
        Dim files As FileInfo()
        Dim index As Integer = 0
    
    
        Private Sub Button1_Click(ByVal sender As Object, ByVal e As EventArgs) Handles Button1.Click
            Dim finfo As New IO.DirectoryInfo(zMailbox)
            files = finfo.GetFiles("*.txt")
            Timer1.Start()
        End Sub
    
        Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
            If index >= files.Length Then
                index = 0
            End If
            TextBox1.Text = files(index).FullPath
            index += 1
        End Sub
    End Class

Tags for this Thread

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