Depending on the details of your process, you might look at your directory and store the file sizes, sleep for 10 seconds, and check for each file if the size has changed. Looking at the file dates might also help. I admit that this may not look very robust, but there are cases where this is sufficient. On some operating systems (VMS, for example), you can't open a file that is being written (it is locked), so you may just try to read the file and you know the file is still being written if you get an exception.

Another possibility, if you control the file writing process, would be to create a second (empty) file having the role of a flag. Create the flag before opening the file for writing, and remove the flag once writing the file is completed. Then you only need to check for the existence of the flag.

Update: When i wrote the above 15 minutes ago, I forgot another possibility, despite the fact that I am using it regularly at work. We are using the flag system described above when we want to know that the writing process has completed all the files that it is supposed to write.

When a file by file check is needed, the solution is even easier: the writing process writes a file with a temporary name (e.g. a .tmp extension) and renames it to its final name once it has completed the task. For example, the writing process write an output.tmp file. Once the file is complete, it renames it as output.txt. Your process then only needs to poll for *.txt files.


In reply to Re: Check if file is written by other process by Laurent_R
in thread Check if file is written by other process by techman2006

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.