title: reliable process to add text on top of existing text-file
body: <p>I know how to add text on top a text file but what I need is a way to ensure that at the point of reading the &quot;old-text&quot; no other process add text to that file. Imagine there exist one log file which will be fed on text from other processes. My current process is using FSO in two steps. The first step Open the text in &quot;read&quot; mode then grab the whole content with <code>.ReadAll</code> and close the file afterwards. The next step is to open this file in &quot;write&quot; mode and add new text on top of the old Content and close the file.</p>
  <p>My problem is that at the moment when I read the whole content another process could write at this time an additional log-entry (which I could not grab). This results in losing the log-entry from the other process. Below is my current workaround...</p>
  <pre><code>Dim strContentsOLD, sNewLogEntry
  Dim sPFnStartLogGol : sPFnStartLogGol = &quot;C:\Path\to\logfile.txt&quot;
  Dim oFSO            : Set oFSO        = CreateObject(&quot;Scripting.FileSystemObject&quot;)
  Dim oLogfile        : Set oLogfile = oFSO.OpenTextFile(sPFnStartLogGol, 1, True)            'ForReading = 1 / True = create new if not exist
  If IsObject(oLogfile) Then                                                                      'already open for Reading
      If oFSO.GetFile(sPFnStartLogGol).Size &gt; 0 Then strContentsOLD = oLogfile.ReadAll            'read all old log-entries
      oLogfile.Close                                                                              'close log-file
      sNewLogEntry = &quot;This is a new Log entry&quot;                                                    'log entry
      Set oLogfile = oFSO.OpenTextFile(sPFnStartLogGol, 2)                                        'open again but ForWriting = 2 
      oLogFile.write sNewLogEntry &amp; vbCrLf &amp; strContentsOLD                                       'add old entries underneath new entry
      oLogFile.close
  End If
  </code></pre>