Results 1 to 14 of 14

Thread: program for monitoring folder

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Jan 2007
    Posts
    82

    Question program for monitoring folder

    Hi every one,

    kindly could you help me to build a program for monitoring specific folder for example :

    C:\MyFolder

    i want the program check the bove folder and give me information about the following:

    • Files with date modified 'current day'
    • The number of files with date current day
    • The size for that files
    • Send that information by e-mail


    I hope it is clear and i hope get your help with this.

    regards,

  2. #2
    Wait... what? weirddemon's Avatar
    Join Date
    Jan 2009
    Location
    USA
    Posts
    3,826

    Re: program for monitoring folder

    There's a component within the VS Toolbox called a File System Watcher.

    You should read up on the documentation and do some research on applicable examples.
    CodeBank contributions: Process Manager, Temp File Cleaner

    Quote Originally Posted by SJWhiteley
    "game trainer" is the same as calling the act of robbing a bank "wealth redistribution"....

  3. #3

    Thread Starter
    Lively Member
    Join Date
    Jan 2007
    Posts
    82

    Re: program for monitoring folder

    thank you for replay.

    but you need to know i am very beginner in vb.net and actully i follow your link and i found the following code but i need your help to add necessary modification as i mention above.


    Code:
    Imports System
    Imports System.IO
    Imports Microsoft.VisualBasic
    Imports System.Security.Permissions
    
    Public Class Watcher
    
        Public Shared Sub Main()
        
             Run()
    
        End Sub
    
        <PermissionSet(SecurityAction.Demand, Name:="FullTrust")> _
        Private Shared Sub Run
    
          Dim args() As String = System.Environment.GetCommandLineArgs()
            ' If a directory is not specified, exit the program.
            If args.Length <> 2 Then
                ' Display the proper way to call the program.
                Console.WriteLine("Usage: Watcher.exe (directory)")
                Return
            End If
    
            ' Create a new FileSystemWatcher and set its properties.
            Dim watcher As New FileSystemWatcher()
            watcher.Path = args(1)
            ' Watch for changes in LastAccess and LastWrite times, and
            ' the renaming of files or directories. 
            watcher.NotifyFilter = (NotifyFilters.LastAccess Or NotifyFilters.LastWrite Or NotifyFilters.FileName 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.EnableRaisingEvents = True
    
            ' Wait for the user to quit the program.
            Console.WriteLine("Press 'q' to quit the sample.")
            While Chr(Console.Read()) <> "q"c
            End While
        End Sub
    
        ' Define the event handlers.
        Private Shared Sub OnChanged(source As Object, e As FileSystemEventArgs)
            ' Specify what is done when a file is changed, created, or deleted.
            Console.WriteLine("File: " & e.FullPath & " " & e.ChangeType)
        End Sub    
    
        Private Shared Sub OnRenamed(source As Object, e As RenamedEventArgs)
            ' Specify what is done when a file is renamed.
            Console.WriteLine("File: {0} renamed to {1}", e.OldFullPath, e.FullPath)
        End Sub
    
    End Class

  4. #4
    Lively Member
    Join Date
    Sep 2009
    Posts
    76

    Re: program for monitoring folder

    Can some one look at this one, i too didn't get it completely. Also How to send email?
    < advertising link removed by moderator >

  5. #5
    PowerPoster 2.0 Negative0's Avatar
    Join Date
    Jun 2000
    Location
    Southeastern MI
    Posts
    4,367

    Re: program for monitoring folder

    I don't think you need the FileSystemWatcher. That is more for raising events when something changes in the folder. If you want something that just reads the files in a folder, then you can use standard file IO functionality:

    Here is an example that outputs some file info to the debug window. This should get you started and give you the correct classes to use.
    Code:
            For Each fileName As String In Directory.GetFiles("C:\Temp")
                Dim fi As New FileInfo(fileName)
                Debug.Print("File:{0} - Created: {1}, Size:{2}", fileName, fi.CreationTime.ToString(), fi.Length)
            Next

  6. #6

    Thread Starter
    Lively Member
    Join Date
    Jan 2007
    Posts
    82

    Re: program for monitoring folder

    Quote Originally Posted by Negative0 View Post
    I don't think you need the FileSystemWatcher. That is more for raising events when something changes in the folder. If you want something that just reads the files in a folder, then you can use standard file IO functionality:

    Here is an example that outputs some file info to the debug window. This should get you started and give you the correct classes to use.
    Code:
            For Each fileName As String In Directory.GetFiles("C:\Temp")
                Dim fi As New FileInfo(fileName)
                Debug.Print("File:{0} - Created: {1}, Size:{2}", fileName, fi.CreationTime.ToString(), fi.Length)
            Next
    thank you very much it is useful but i face the following problem:

    the file size show as byte. how i convert it to KB or MB?

    i want the program show the file of today only(current day).

    i want to send the output as e-mail.

    thank you all.

  7. #7
    Addicted Member
    Join Date
    Dec 2008
    Posts
    185

    Re: program for monitoring folder

    Not sure if there is anything to do that for you... but it is simple math:

    Code:
    public function GetFriendlyFileSize(byval filebytesize as integer) as string
      Dim KiloBytes As Double = filebytesize / 1024
      If KiloBytes >= 1024 Then
        Dim MegaBytes As Double = KiloBytes / 1024
        If MegaBytes >= 1024 Then
          Dim GigaBytes As Double = MegaBytes / 1024
          Return String.Format("{0} gb", GigaBytes.ToString)
        Else
          Return String.Format("{0} mb", MegaBytes.ToString)
        End If
      Else
       Return String.Format("{0} kb", KiloBytes.ToString)
      End If
    end function

  8. #8
    PowerPoster 2.0 Negative0's Avatar
    Join Date
    Jun 2000
    Location
    Southeastern MI
    Posts
    4,367

    Re: program for monitoring folder

    If you want to only get the files for today, then check the created date and see if it is today. To send an email, you can use the SMTP client class, but you will need an SMTP server to use to send the email.

  9. #9

    Thread Starter
    Lively Member
    Join Date
    Jan 2007
    Posts
    82

    Re: program for monitoring folder

    Quote Originally Posted by Negative0 View Post
    If you want to only get the files for today, then check the created date and see if it is today. To send an email, you can use the SMTP client class, but you will need an SMTP server to use to send the email.
    thank you,

    i found the following code using to send e-mail and it is work :

    Code:
    Imports System.Net.Mail
    Public Class Form1
    
        Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            Dim mail As New MailMessage()
            Dim SmtpServer As New SmtpClient()
    
            SmtpServer.Credentials = New Net.NetworkCredential("[email protected]", "bbbbbbbbb")
            SmtpServer.Port = 25
            SmtpServer.Host = "smtp.gmail.com"
            SmtpServer.EnableSsl = True
    
    
            mail.From = New MailAddress("MyEmail@gmail", "TRY")
            mail.To.Add("MyEmail@gmail")
            'mail.CC.Add("mail2")
            'mail.CC.Add("mail3")
            'mail.Bcc.Add("mail5")
    
            mail.Subject = "TRy TRY"
            mail.Body = "bady_test"
            mail.IsBodyHtml = True 'Change it as you need
            mail.Attachments.Add(New Attachment("c:\MyFolder\a.txt")) 'The path of the file to be attached
            mail.DeliveryNotificationOptions = DeliveryNotificationOptions.OnFailure
    
            SmtpServer.Send(mail)
    
        End Sub
    End Class
    but i need to put this code with following program to be able to send the information by e-mail :

    Code:
    Imports System
    Imports System.IO
    Imports Microsoft.VisualBasic
    Imports System.Security.Permissions
    Imports System.Net.Mail
    Module Module1
    
        Sub Main()
    
            For Each fileName As String In Directory.GetFiles("C:\MyFolder")
                Dim fi As New FileInfo(fileName)
                'Debug.Print("File:{0} - Created: {1}, Size:{2}", fileName, fi.CreationTime.ToString(), fi.Length)
                Console.WriteLine("File:{0} - Created: {1}, Size:{2}KB", fileName, fi.CreationTime.ToString(), (fi.Length) \ 1024)
            Next
            Console.WriteLine("Press 'q' to quit the sample.")
            While Chr(Console.Read()) <> "q"
            End While
    
        End Sub
        
    End Module
    kindly help me to solve this problem.

    thank you.

  10. #10

    Thread Starter
    Lively Member
    Join Date
    Jan 2007
    Posts
    82

    Re: program for monitoring folder

    Dear all,

    i still wait your help with this issue.

    regards,

  11. #11
    PowerPoster 2.0 Negative0's Avatar
    Join Date
    Jun 2000
    Location
    Southeastern MI
    Posts
    4,367

    Re: program for monitoring folder

    So what issue are you having? If your email code works and your reading folder code works, what is hard about getting them to work together?

  12. #12
    New Member
    Join Date
    Feb 2010
    Posts
    13

    Re: program for monitoring folder

    Quote Originally Posted by mse07 View Post
    Dear all,

    i still wait your help with this issue.

    regards,
    Think about it for a second mse07, instead of writing the file information to the console, you can 'write' it to the body of the email.

  13. #13

    Thread Starter
    Lively Member
    Join Date
    Jan 2007
    Posts
    82

    Re: program for monitoring folder

    Quote Originally Posted by Negative0 View Post
    So what issue are you having? If your email code works and your reading folder code works, what is hard about getting them to work together?
    Quote Originally Posted by Bewl View Post
    Think about it for a second mse07, instead of writing the file information to the console, you can 'write' it to the body of the email.
    the problem is : i want the output of the print command write in e-mail body.

    Code:
    Imports System
    Imports System.IO
    Imports Microsoft.VisualBasic
    Imports System.Security.Permissions
    Imports System.Net.Mail
    ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
    Module Module1
    
        Sub Main()
    
            For Each fileName As String In Directory.GetFiles("C:\MyFolder")
                Dim fi As New FileInfo(fileName)
               
                Console.WriteLine("File:{0} - Created: {1}, Size:{2}KB", fileName, fi.CreationTime.ToString(), (fi.Length) \ 1024)
    
            Next
    ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
            Dim mail As New MailMessage()
            Dim SmtpServer As New SmtpClient()
    
            SmtpServer.Credentials = New Net.NetworkCredential("[email protected]", "abcd")
            SmtpServer.Port = 25
            SmtpServer.Host = "smtp.gmail.com"
            SmtpServer.EnableSsl = True
    
    
            mail.From = New MailAddress("Mymail@gmail", "TRY")
            mail.To.Add("Mymail@gmail")
            'mail.CC.Add("mail2")
            'mail.CC.Add("mail3")
            'mail.Bcc.Add("mail5")
            mail.Subject = ""
    
            mail.IsBodyHtml = True 
            mail.Attachments.Add(New Attachment("c:\MyFolder\a.txt"))
    
            mail.Body = ""
            mail.DeliveryNotificationOptions = DeliveryNotificationOptions.OnFailure
    
            SmtpServer.Send(mail)
    
        End Sub
    
        
    End Module

  14. #14
    New Member
    Join Date
    Feb 2010
    Posts
    13

    Re: program for monitoring folder

    Quote Originally Posted by mse07 View Post
    the problem is : i want the output of the print command write in e-mail body.

    Code:
    Imports System
    Imports System.IO
    Imports Microsoft.VisualBasic
    Imports System.Security.Permissions
    Imports System.Net.Mail
    ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
    Module Module1
    
        Sub Main()
    
            For Each fileName As String In Directory.GetFiles("C:\MyFolder")
                Dim fi As New FileInfo(fileName)
               
                Console.WriteLine("File:{0} - Created: {1}, Size:{2}KB", fileName, fi.CreationTime.ToString(), (fi.Length) \ 1024)
    
            Next
    ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
            Dim mail As New MailMessage()
            Dim SmtpServer As New SmtpClient()
    
            SmtpServer.Credentials = New Net.NetworkCredential("[email protected]", "abcd")
            SmtpServer.Port = 25
            SmtpServer.Host = "smtp.gmail.com"
            SmtpServer.EnableSsl = True
    
    
            mail.From = New MailAddress("Mymail@gmail", "TRY")
            mail.To.Add("Mymail@gmail")
            'mail.CC.Add("mail2")
            'mail.CC.Add("mail3")
            'mail.Bcc.Add("mail5")
            mail.Subject = ""
    
            mail.IsBodyHtml = True 
            mail.Attachments.Add(New Attachment("c:\MyFolder\a.txt"))
    
            mail.Body = ""
            mail.DeliveryNotificationOptions = DeliveryNotificationOptions.OnFailure
    
            SmtpServer.Send(mail)
    
        End Sub
    
        
    End Module
    here is a hint. Turn this into a string variable:
    Code:
    1. Console.WriteLine("File:{0} - Created: {1}, Size:{2}KB", fileName, fi.CreationTime.ToString(), (fi.Length) \ 1024)

    Then take that Variable and send it to the body of the email.

    If you dont understand this, then you might want to visit (or revisit) the basics of Visual Basic.

    People who take snippets of code from other people in attempt to create their own modifications without the basic understandings of the foundations of programming tend to run into problems like this. I strongly suggest reading the first chapter or two of any Visual Basic book (or any programming book for that matter), and I think you will understand why I am writing this.
    Last edited by Bewl; Mar 7th, 2010 at 01:57 AM.

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