Looking for contents in a file every minute

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Andy B

    #1

    Looking for contents in a file every minute

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



  • James Hahn

    #2
    Re: Looking for contents in a file every minute

    http://msdn.microsoft.com/en-us/libr...emwatcher.aspx
    FileSystemWatch er Class

    "Andy B" <a_borka@sbcglo bal.netwrote in message
    news:%238ulBQoJ JHA.4600@TK2MSF TNGP06.phx.gbl. ..
    What would i use to look for changes in a file every minute?
    >

    Comment

    • Michel Posseth  [MCP]

      #3
      Re: Looking for contents in a file every minute

      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]








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

      Comment

      • Andy B

        #4
        Re: Looking for contents in a file every minute

        I need to look for things inside the file every minute even if the file
        hasn't changed..
        "Michel Posseth [MCP]" <MSDN@posseth.c omwrote in message
        news:%23iw49isJ JHA.5576@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]

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

        Comment

        • kimiraikkonen

          #5
          Re: Looking for contents in a file every minute

          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:%23 iw49isJJHA.5576 @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.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_bo...@sbcglo bal.netschreef in bericht
          news:%238ulBQoJ JHA.4600@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

          Comment

          • rowe_newsgroups

            #6
            Re: Looking for contents in a file every minute

            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:%23 iw49isJJHA.5576 @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.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_bo...@sbcglo bal.netschreef in bericht
            news:%238ulBQoJ JHA.4600@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]

            Comment

            • Michel Posseth  [MCP]

              #7
              Re: Looking for contents in a file every minute

              >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_borka@sbcglo bal.netschreef in bericht
              news:OKD1r15JJH A.5920@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]" <MSDN@posseth.c omwrote in message
              news:%23iw49isJ JHA.5576@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_borka@sbcglo bal.netschreef in bericht
              >news:%238ulBQo JJHA.4600@TK2MS FTNGP06.phx.gbl ...
              >>What would i use to look for changes in a file every minute?
              >>>
              >>>
              >>>
              >>
              >>
              >
              >

              Comment

              Working...