-
Jan 11th, 2015, 05:44 PM
#1
Thread Starter
Hyperactive Member
[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
-
Jan 11th, 2015, 10:23 PM
#2
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.
-
Jan 12th, 2015, 04:39 AM
#3
Thread Starter
Hyperactive Member
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
Originally Posted by jmcilhinney
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.
-
Jan 12th, 2015, 06:41 AM
#4
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
-
Jan 12th, 2015, 08:22 AM
#5
Thread Starter
Hyperactive Member
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.
- I have a Folder C\Test with txt files.
- Get path of first text file.
- wait 5 mins
- Get path of second text file
- wait 5 mins
- get path of third file
- wait 5 mins
- do same for all files in folder
- stop !
I hope its more explicit now. Thanks
-
Jan 12th, 2015, 08:26 AM
#6
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
-
Jan 12th, 2015, 09:18 AM
#7
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."
-
Jan 12th, 2015, 09:27 AM
#8
Thread Starter
Hyperactive Member
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
-
Jan 12th, 2015, 09:32 AM
#9
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
-
Jan 12th, 2015, 10:01 AM
#10
Thread Starter
Hyperactive Member
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
Originally Posted by techgnome
-
Jan 12th, 2015, 10:13 AM
#11
Re: Get files from Folder using timer event
Don't worry, we have plenty of band-aids around here to help when needed.
-tg
-
Jan 12th, 2015, 11:36 AM
#12
Thread Starter
Hyperactive Member
Re: Get files from Folder using timer event
Originally Posted by techgnome
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
-
Jan 12th, 2015, 12:28 PM
#13
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
-
Jan 12th, 2015, 12:44 PM
#14
Thread Starter
Hyperactive Member
Re: Get files from Folder using timer event
Originally Posted by techgnome
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
-
Jan 12th, 2015, 02:10 PM
#15
Thread Starter
Hyperactive Member
Re: Get files from Folder using timer event
Originally Posted by tendemo
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.
-
Jan 12th, 2015, 02:35 PM
#16
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
-
Jan 12th, 2015, 02:49 PM
#17
Thread Starter
Hyperactive Member
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.
-
Jan 12th, 2015, 03:51 PM
#18
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
-
Jan 12th, 2015, 04:35 PM
#19
Thread Starter
Hyperactive Member
Re: Get files from Folder using timer event
Originally Posted by techgnome
"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
-
Jan 12th, 2015, 05:10 PM
#20
Thread Starter
Hyperactive Member
Re: Get files from Folder using timer event
Did an F12 and hit run and all was well, so many thanks again
Originally Posted by tendemo
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|