473,545 Members | 666 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Looking for contents in a file every minute

What would i use to look for changes in a file every minute?

Oct 5 '08 #1
6 1685
http://msdn.microsoft.com/en-us/libr...emwatcher.aspx
FileSystemWatch er Class

"Andy B" <a_*****@sbcglo bal.netwrote in message
news:%2******** ********@TK2MSF TNGP06.phx.gbl. ..
What would i use to look for changes in a file every minute?
Oct 5 '08 #2
Hello Andy ,

If you want an event driven aproach go for the file system watcher as this
can give you an event when a change took place in a file
so also when there are 2 changes within a minute you get 2 change events .

Ofcourse you could also just use a timer , read the file in a string
variabel and compare the 2 strings if the 2 strings are not the same the
file has changed
however this wil cost you more CPU / IO cycles
Use the filesystem watcher to get notified when a change on the file took
place , and then reread / process the file
http://msdn.microsoft.com/en-us/libr...emwatcher.aspx

in the link i provided ( also in your previous post ) there is example code
included
in case you missed it
Public Class Watcher

Public Shared Sub Main()

Run()

End Sub

<PermissionSet( SecurityAction. Demand, Name:="FullTrus t")_
Private Shared Sub Run

Dim args() As String = System.Environm ent.GetCommandL ineArgs()
' If a directory is not specified, exit the program.
If args.Length <2 Then
' Display the proper way to call the program.
Console.WriteLi ne("Usage: Watcher.exe (directory)")
Return
End If

' Create a new FileSystemWatch er and set its properties.
Dim watcher As New FileSystemWatch er()
watcher.Path = args(1)
' Watch for changes in LastAccess and LastWrite times, and
' the renaming of files or directories.
watcher.NotifyF ilter = (NotifyFilters. LastAccess Or
NotifyFilters.L astWrite Or NotifyFilters.F ileName Or
NotifyFilters.D irectoryName)
' Only watch text files.
watcher.Filter = "*.txt"

' Add event handlers.
AddHandler watcher.Changed , AddressOf OnChanged
AddHandler watcher.Created , AddressOf OnChanged
AddHandler watcher.Deleted , AddressOf OnChanged
AddHandler watcher.Renamed , AddressOf OnRenamed

' Begin watching.
watcher.EnableR aisingEvents = True

' Wait for the user to quit the program.
Console.WriteLi ne("Press 'q' to quit the sample.")
While Chr(Console.Rea d()) <"q"c
End While
End Sub

' Define the event handlers.
Private Shared Sub OnChanged(sourc e As Object, e As FileSystemEvent Args)
' Specify what is done when a file is changed, created, or deleted.
Console.WriteLi ne("File: " & e.FullPath & " " & e.ChangeType)
End Sub

Private Shared Sub OnRenamed(sourc e As Object, e As RenamedEventArg s)
' Specify what is done when a file is renamed.
Console.WriteLi ne("File: {0} renamed to {1}", e.OldFullPath,
e.FullPath)
End Sub

End Class

Michel Posseth [MCP]
http://www.vbdotnetcoder.com



"Andy B" <a_*****@sbcglo bal.netschreef in bericht
news:%2******** ********@TK2MSF TNGP06.phx.gbl. ..
What would i use to look for changes in a file every minute?

Oct 5 '08 #3
I need to look for things inside the file every minute even if the file
hasn't changed..
"Michel Posseth [MCP]" <MS**@posseth.c omwrote in message
news:%2******** ********@TK2MSF TNGP05.phx.gbl. ..
Hello Andy ,

If you want an event driven aproach go for the file system watcher as this
can give you an event when a change took place in a file
so also when there are 2 changes within a minute you get 2 change events .

Ofcourse you could also just use a timer , read the file in a string
variabel and compare the 2 strings if the 2 strings are not the same the
file has changed
however this wil cost you more CPU / IO cycles
Use the filesystem watcher to get notified when a change on the file took
place , and then reread / process the file
http://msdn.microsoft.com/en-us/libr...emwatcher.aspx

in the link i provided ( also in your previous post ) there is example
code included
in case you missed it
Public Class Watcher

Public Shared Sub Main()

Run()

End Sub

<PermissionSet( SecurityAction. Demand, Name:="FullTrus t")_
Private Shared Sub Run

Dim args() As String = System.Environm ent.GetCommandL ineArgs()
' If a directory is not specified, exit the program.
If args.Length <2 Then
' Display the proper way to call the program.
Console.WriteLi ne("Usage: Watcher.exe (directory)")
Return
End If

' Create a new FileSystemWatch er and set its properties.
Dim watcher As New FileSystemWatch er()
watcher.Path = args(1)
' Watch for changes in LastAccess and LastWrite times, and
' the renaming of files or directories.
watcher.NotifyF ilter = (NotifyFilters. LastAccess Or
NotifyFilters.L astWrite Or NotifyFilters.F ileName Or
NotifyFilters.D irectoryName)
' Only watch text files.
watcher.Filter = "*.txt"

' Add event handlers.
AddHandler watcher.Changed , AddressOf OnChanged
AddHandler watcher.Created , AddressOf OnChanged
AddHandler watcher.Deleted , AddressOf OnChanged
AddHandler watcher.Renamed , AddressOf OnRenamed

' Begin watching.
watcher.EnableR aisingEvents = True

' Wait for the user to quit the program.
Console.WriteLi ne("Press 'q' to quit the sample.")
While Chr(Console.Rea d()) <"q"c
End While
End Sub

' Define the event handlers.
Private Shared Sub OnChanged(sourc e As Object, e As
FileSystemEvent Args)
' Specify what is done when a file is changed, created, or deleted.
Console.WriteLi ne("File: " & e.FullPath & " " & e.ChangeType)
End Sub

Private Shared Sub OnRenamed(sourc e As Object, e As RenamedEventArg s)
' Specify what is done when a file is renamed.
Console.WriteLi ne("File: {0} renamed to {1}", e.OldFullPath,
e.FullPath)
End Sub

End Class

Michel Posseth [MCP]
http://www.vbdotnetcoder.com



"Andy B" <a_*****@sbcglo bal.netschreef in bericht
news:%2******** ********@TK2MSF TNGP06.phx.gbl. ..
>What would i use to look for changes in a file every minute?


Oct 6 '08 #4
On Oct 6, 1:22*pm, "Andy B" <a_bo...@sbcglo bal.netwrote:
I need to look for things inside the file every minute even if the file
hasn't changed..
"Michel Posseth [MCP]" <M...@posseth.c omwrote in messagenews:%2* *************** @TK2MSFTNGP05.p hx.gbl...
Hello Andy ,
If you want an event driven aproach go for the file system watcher as this
can give you an event when a change took place in a file
so also when there are 2 changes within a minute you get 2 change events .
Ofcourse you could also just use a timer , read the file in a string
variabel and compare the 2 strings if the 2 strings are not the same the
file has changed
however this wil cost you more CPU / IO cycles
Use the filesystem watcher to get notified when a change on the file took
place , and then reread / process *the file
http://msdn.microsoft.com/en-us/libr...emwatcher.aspx
in the link i provided ( also in your previous post ) there is example
code included
in case you missed it
Public Class Watcher
* *Public Shared Sub Main()
* * * * Run()
* *End Sub
* *<PermissionSet (SecurityAction .Demand, Name:="FullTrus t")_
* *Private Shared Sub Run
* * *Dim args() As String = System.Environm ent.GetCommandL ineArgs()
* * * *' If a directory is not specified, exit the program.
* * * *If args.Length <2 Then
* * * * * *' Display the proper way to call the program.
* * * * * *Console.WriteL ine("Usage: Watcher.exe (directory)")
* * * * * *Return
* * * *End If
* * * *' Create a new FileSystemWatch er and set its properties.
* * * *Dim watcher As New FileSystemWatch er()
* * * *watcher.Path = args(1)
* * * *' Watch for changes in LastAccess and LastWrite times, and
* * * *' the renaming of files or directories.
* * * *watcher.Notify Filter = (NotifyFilters. LastAccess Or
NotifyFilters.L astWrite Or NotifyFilters.F ileName Or
NotifyFilters.D irectoryName)
* * * *' Only watch text files.
* * * *watcher.Filter = "*.txt"
* * * *' Add event handlers.
* * * *AddHandler watcher.Changed , AddressOf OnChanged
* * * *AddHandler watcher.Created , AddressOf OnChanged
* * * *AddHandler watcher.Deleted , AddressOf OnChanged
* * * *AddHandler watcher.Renamed , AddressOf OnRenamed
* * * *' Begin watching.
* * * *watcher.Enable RaisingEvents = True
* * * *' Wait for the user to quit the program.
* * * *Console.WriteL ine("Press 'q' to quit the sample.")
* * * *While Chr(Console.Rea d()) <"q"c
* * * *End While
* *End Sub
* *' Define the event handlers.
* *Private Shared Sub OnChanged(sourc e As Object, e As
FileSystemEvent Args)
* * * *' Specify what is done when a file is changed, created, or deleted.
* * * *Console.WriteL ine("File: " & e.FullPath & " " & e.ChangeType)
* *End Sub
* *Private Shared Sub OnRenamed(sourc e As Object, e As RenamedEventArg s)
* * * *' Specify what is done when a file is renamed.
* * * *Console.WriteL ine("File: {0} renamed to {1}", e.OldFullPath,
e.FullPath)
* *End Sub
End Class
Michel Posseth [MCP]
http://www.vbdotnetcoder.com
"Andy B" <a_bo...@sbcglo bal.netschreef in bericht
news:%2******** ********@TK2MSF TNGP06.phx.gbl. ..
What would i use to look for changes in a file every minute?- Hide quoted text -

- Show quoted text -
Hi,
You can use a Timer with 60000-milisecond intervals without needing to
raise changed events. However you need to determine what you really
mean by calling "inside of file" and what you want to look for, for
example, if you're just intending to check a file's LastAccessTime,
you can try to use that:

Untested:

'-----------------------------------------------------
' File to check in every 60 seconds
Dim myfile As New FileInfo("c:\fi le.exe")

' Optional ArrayList to log LastAccessTime
Dim logArrList As New ArrayList

' Assuming your timer's interval is 60000 ms
Private Sub Timer1_Tick(ByV al sender As Object, _
ByVal System.EventArg s) Handles Timer1.Tick

' Preferably add the last entry to an ArrayList
' to log item as DateTime type

logArrList.Add( myfile.LastAcce ssTime)

End Sub
'--------------------------------------------------------
Note that, with the code above, the LastAccessTime will be added your
ArrayList eventhough your file's LastAccessTime doesn't change. But
you must be sure to use that because of consuming excessive I/O cycle
and system resources in every 1 minute.

Hope this helps,

Onur Güzel
Oct 6 '08 #5
On Oct 6, 6:22*am, "Andy B" <a_bo...@sbcglo bal.netwrote:
I need to look for things inside the file every minute even if the file
hasn't changed..
"Michel Posseth [MCP]" <M...@posseth.c omwrote in messagenews:%2* *************** @TK2MSFTNGP05.p hx.gbl...
Hello Andy ,
If you want an event driven aproach go for the file system watcher as this
can give you an event when a change took place in a file
so also when there are 2 changes within a minute you get 2 change events .
Ofcourse you could also just use a timer , read the file in a string
variabel and compare the 2 strings if the 2 strings are not the same the
file has changed
however this wil cost you more CPU / IO cycles
Use the filesystem watcher to get notified when a change on the file took
place , and then reread / process *the file
http://msdn.microsoft.com/en-us/libr...emwatcher.aspx
in the link i provided ( also in your previous post ) there is example
code included
in case you missed it
Public Class Watcher
* *Public Shared Sub Main()
* * * * Run()
* *End Sub
* *<PermissionSet (SecurityAction .Demand, Name:="FullTrus t")_
* *Private Shared Sub Run
* * *Dim args() As String = System.Environm ent.GetCommandL ineArgs()
* * * *' If a directory is not specified, exit the program.
* * * *If args.Length <2 Then
* * * * * *' Display the proper way to call the program.
* * * * * *Console.WriteL ine("Usage: Watcher.exe (directory)")
* * * * * *Return
* * * *End If
* * * *' Create a new FileSystemWatch er and set its properties.
* * * *Dim watcher As New FileSystemWatch er()
* * * *watcher.Path = args(1)
* * * *' Watch for changes in LastAccess and LastWrite times, and
* * * *' the renaming of files or directories.
* * * *watcher.Notify Filter = (NotifyFilters. LastAccess Or
NotifyFilters.L astWrite Or NotifyFilters.F ileName Or
NotifyFilters.D irectoryName)
* * * *' Only watch text files.
* * * *watcher.Filter = "*.txt"
* * * *' Add event handlers.
* * * *AddHandler watcher.Changed , AddressOf OnChanged
* * * *AddHandler watcher.Created , AddressOf OnChanged
* * * *AddHandler watcher.Deleted , AddressOf OnChanged
* * * *AddHandler watcher.Renamed , AddressOf OnRenamed
* * * *' Begin watching.
* * * *watcher.Enable RaisingEvents = True
* * * *' Wait for the user to quit the program.
* * * *Console.WriteL ine("Press 'q' to quit the sample.")
* * * *While Chr(Console.Rea d()) <"q"c
* * * *End While
* *End Sub
* *' Define the event handlers.
* *Private Shared Sub OnChanged(sourc e As Object, e As
FileSystemEvent Args)
* * * *' Specify what is done when a file is changed, created, or deleted.
* * * *Console.WriteL ine("File: " & e.FullPath & " " & e.ChangeType)
* *End Sub
* *Private Shared Sub OnRenamed(sourc e As Object, e As RenamedEventArg s)
* * * *' Specify what is done when a file is renamed.
* * * *Console.WriteL ine("File: {0} renamed to {1}", e.OldFullPath,
e.FullPath)
* *End Sub
End Class
Michel Posseth [MCP]
http://www.vbdotnetcoder.com
"Andy B" <a_bo...@sbcglo bal.netschreef in bericht
news:%2******** ********@TK2MSF TNGP06.phx.gbl. ..
What would i use to look for changes in a file every minute?
You should be able to make the assumption that what you have is
correct (as the file hasn't changed) if the FileSystemWatch er hasn't
told you otherwise.

Thanks,

Seth Rowe [MVP]
http://sethrowe.blogspot.com/
Oct 6 '08 #6
>I need to look for things inside the file every minute even if the file
>hasn't changed..
Huh !? why would you do so ,why would a program need to reread the same
values every minute ???
hint : if the contents / values in the file change the filesystem watcher
would raise an event

HTH
Michel


"Andy B" <a_*****@sbcglo bal.netschreef in bericht
news:OK******** ******@TK2MSFTN GP03.phx.gbl...
>I need to look for things inside the file every minute even if the file
hasn't changed..
"Michel Posseth [MCP]" <MS**@posseth.c omwrote in message
news:%2******** ********@TK2MSF TNGP05.phx.gbl. ..
>Hello Andy ,

If you want an event driven aproach go for the file system watcher as
this can give you an event when a change took place in a file
so also when there are 2 changes within a minute you get 2 change events
.

Ofcourse you could also just use a timer , read the file in a string
variabel and compare the 2 strings if the 2 strings are not the same the
file has changed
however this wil cost you more CPU / IO cycles
Use the filesystem watcher to get notified when a change on the file took
place , and then reread / process the file
http://msdn.microsoft.com/en-us/libr...emwatcher.aspx

in the link i provided ( also in your previous post ) there is example
code included
in case you missed it
Public Class Watcher

Public Shared Sub Main()

Run()

End Sub

<PermissionSet( SecurityAction. Demand, Name:="FullTrus t")_
Private Shared Sub Run

Dim args() As String = System.Environm ent.GetCommandL ineArgs()
' If a directory is not specified, exit the program.
If args.Length <2 Then
' Display the proper way to call the program.
Console.WriteLi ne("Usage: Watcher.exe (directory)")
Return
End If

' Create a new FileSystemWatch er and set its properties.
Dim watcher As New FileSystemWatch er()
watcher.Path = args(1)
' Watch for changes in LastAccess and LastWrite times, and
' the renaming of files or directories.
watcher.NotifyF ilter = (NotifyFilters. LastAccess Or
NotifyFilters. LastWrite Or NotifyFilters.F ileName Or
NotifyFilters. DirectoryName)
' Only watch text files.
watcher.Filter = "*.txt"

' Add event handlers.
AddHandler watcher.Changed , AddressOf OnChanged
AddHandler watcher.Created , AddressOf OnChanged
AddHandler watcher.Deleted , AddressOf OnChanged
AddHandler watcher.Renamed , AddressOf OnRenamed

' Begin watching.
watcher.EnableR aisingEvents = True

' Wait for the user to quit the program.
Console.WriteLi ne("Press 'q' to quit the sample.")
While Chr(Console.Rea d()) <"q"c
End While
End Sub

' Define the event handlers.
Private Shared Sub OnChanged(sourc e As Object, e As
FileSystemEven tArgs)
' Specify what is done when a file is changed, created, or
deleted.
Console.WriteLi ne("File: " & e.FullPath & " " & e.ChangeType)
End Sub

Private Shared Sub OnRenamed(sourc e As Object, e As RenamedEventArg s)
' Specify what is done when a file is renamed.
Console.WriteLi ne("File: {0} renamed to {1}", e.OldFullPath,
e.FullPath)
End Sub

End Class

Michel Posseth [MCP]
http://www.vbdotnetcoder.com



"Andy B" <a_*****@sbcglo bal.netschreef in bericht
news:%2******* *********@TK2MS FTNGP06.phx.gbl ...
>>What would i use to look for changes in a file every minute?



Oct 6 '08 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
2576
by: Mike | last post by:
Hello, I'm looking to create a PHP script that will automatically generate an index/menu/list (whatever) based on the PDF files that are within a particular directory. I would like the script to be able to parse out the title, description, author(s), and date from the documents, and use that information to create the index. Any...
8
1643
by: Phoenix | last post by:
Here's a challenge that is killing me: I've got 2 web servers and a SQL Server and about 5,000 'users' who stay connected to the site all day. I have a page that is supposed to be 'real-time', so to do this, I have a 1px frame that refreshes every 15 seconds (so the other frame doesn't have to reload all the time--the top only reloads when...
14
16410
by: Peter CCH | last post by:
Database log of my DB is around 2GB. The database is using FULL recovery option. I want to reduce the file size of the log cause it takes up a lot of space. I'd do a full database backup, then backup the transaction log as well .... both backup performed with a check on the option "clear inactive entries from transaction log".
2
1625
by: blip | last post by:
Is this acceptable ? It seems too easy and too simple... #include<iostream> #include<fstream> #include<cstdlib> #include<string> struct Person{ char name ; int age ;
12
2549
by: Boobie | last post by:
Looking for a REALLY BUSY, really active RSS feed (nothing xxx pls ;) Anyone knows of free one ? One that returns at least 20 new feeds every minute or so ? Need to test out an app. Thanks
8
2768
by: Cider123 | last post by:
I ran into a situation where my Window Service had to process 100,000+ files, when I first noticed I needed to tweak various routines. Everything runs fine, but here's what I ran into: In the routine that loops through the file buffer: for (int i=0;i < _Buffer.length; i++) { // Code here
8
2489
by: gumi | last post by:
Hi, I am looking for code for a alarm clock program that pops up a messege to be used as part of my VB.Net class project. Any help is very much appreciated. Thanks
8
1164
by: Jonathan Wood | last post by:
I'm having a difficult time making the transition to .NET. I came up with some projects to get me started and, in every case, they involved tasks that were apparently difficult to accomplish and/or difficult to get information about. One item is a menu that is based on a database. My thinking is that I would create some custom controls that...
0
7467
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7807
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7419
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
7756
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
5971
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
0
3442
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1879
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1014
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
703
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.