Thread: detecting new files

  1. #1
    Registered User
    Join Date
    Jul 2003
    Posts
    6

    detecting new files

    Hello all,

    I'm wondering if anyone has a more elegant solution to a pretty simple problem... does anyone have any ideas how to continuously monitor a directory for new files? The only way I can think of right now would be to poll a directory, process the file, delete it, and then sleep for a bit and check the directory again.

    I mean, it works, but I can't help the feeling there's a better way of doing this... any ideas?

    Thanks in advance!

  2. #2
    Registered User
    Join Date
    Jun 2003
    Posts
    245
    OS ?

  3. #3
    Registered User
    Join Date
    Jul 2003
    Posts
    6
    Whoops... sorry about that... used to Java and the whole 'platform independent' thing...

    Looking for a Solaris solution...

  4. #4
    Registered User
    Join Date
    Jun 2003
    Posts
    245
    In that case, maybe you want to look at the Sendmail source code - I'm not exactly sure how it works, but I assume it has some kind of algorithm that you describe for it's mail pool.

  5. #5
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    >continuously monitor a directory for new files?

    New files or updated files? If new files, then here's one idea:

    First count the files in the directory, sleep for a bit, then count the files in the directory again.

  6. #6
    Registered User
    Join Date
    Jul 2003
    Posts
    18
    It depends on the platform on which you want to implement this.
    On Unix and associated flavors you could use a thread that starts at system startup and then forks a child process everytime a new file is created in the directory and this child process could take appropriate action as required.

    Hope this suffices.

  7. #7
    Registered User
    Join Date
    Jul 2003
    Posts
    6
    Hey all,

    Thanks for the ideas!

  8. #8
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    >>First count the files in the directory, sleep for a bit, then count the files in the directory again.<<
    This won't work if one file gets deleted and another gets created, as you'll still have the same amount of files at the end.

    One trouble with polling for files is that you never know if the file creator has actually finished writing to it. You could easily end up processing half a file, and confusing the hell out of the creator process.

    To get round this problem (in the scripting world, as opposed to C), I've used files with specific names that denote that another file is ready for processing. For example, the data file might be called mydata.dat, and when the creating process has finished with it, it simply touches an empty file called mydata.dat.ready. This still means a polling process, but you'd only be looking for files named *.ready.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  9. #9
    Registered User
    Join Date
    Jun 2003
    Posts
    245
    why not just create the file, wirte the data to it, and when finished, rather than create another file, just rename the existing file?

  10. #10
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Originally posted by _Elixia_
    why not just create the file, wirte the data to it, and when finished, rather than create another file, just rename the existing file?
    Yeah, that'd do. My suggestion was based on having the ability to have multiple files (different names) being created, and a .ready file denoting that the batch of files is ready, instead of a single one. I think I should'a said that before

    Something like
    Code:
    Names.dat
    Addresses.dat
    PhoneNumbers.dat
    AllFiles.ready
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Reading .dat files from a folder in current directory...
    By porsche911nfs in forum C++ Programming
    Replies: 7
    Last Post: 04-04-2009, 09:52 PM
  2. Linking header files, Source files and main program(Accel. C++)
    By Daniel Primed in forum C++ Programming
    Replies: 3
    Last Post: 01-17-2006, 11:46 AM
  3. Multiple Cpp Files
    By w4ck0z in forum C++ Programming
    Replies: 5
    Last Post: 11-14-2005, 02:41 PM
  4. Folding@Home Cboard team?
    By jverkoey in forum A Brief History of Cprogramming.com
    Replies: 398
    Last Post: 10-11-2005, 08:44 AM
  5. Detecting Changes in Files
    By globalerr_h in forum C++ Programming
    Replies: 0
    Last Post: 11-15-2002, 12:32 PM