title: FileSystemObject "Permission Denied" on DeleteFile and CreateTextFile
body: <p>I am investigating a problem with an old VB6 app which has just started happening on the dedicated XP machine it runs on. The application processes email messages and for each items it writes a file to a local directory, passes the file path to another function for processing then deletes the file via a Kill command. </p>
  
  <p>It all seems to work until an error occurs during processing. The error handler has no explicit Kill or DeleteFile in it, the error is just handled and processing continues with the next message. As soon as that occurs, the CreateTextFile method fails for every subsequent item with a 'Permission denied' error as the file already exists (even though the CreateTextFile call passes True for overwrite). </p>
  
  <p>I have tried various things, most notably putting Kill, FSO.DeleteFile or a call to the DeleteFile API in the error handler, yet none of these work (first two fail with permission denied, the API doesn't error but doesn't delete the file either).</p>
  
  <p>I've installed Unlocker on the machine and the only process with a lock on the file is the application in question. Filemon reports a 'sharing violation' on the file following an 'IRP_MJ_CREATE' as soon as the problem occurs so I guess this might be pointing to the issue but I cannot see what the problem actually is.</p>
  
  <p>The simplified code is as follows:</p>
  
  <pre><code>Do While objMessages.Count &gt; 0
      Set fsObj = CreateObject("Scripting.FileSystemObject")
      Set tsObj = fsObj.CreateTextFile("C:\Temp\MyFile.txt", True)
      ...
      tsObj.Close
      Set tsObj = Nothing
      Set fsObj = Nothing
      ...
      &lt;some processing which may raise an error&gt;
      ...
      Kill ("C:\Temp\MyFile.txt")    ' Works if no error raised &amp; processing continues ok
      ...
      ...
  Loop
  
  ErrorHandler:
      Kill ("C:\Temp\MyFile.txt")                             ' Permission Denied
      Set fsObj = CreateObject("Scripting.FileSystemObject")
      fsObj.DeleteFile("C:\Temp\MyFile.txt", True)            ' Permission Denied 
      Dim lRet As Long
      lRet = DeleteFile(gstrBodyTextFile)                     ' Nothing done
      ' After error, processing returns to calling procedure 
      ' which is in a loop and calls back into here and starts
      ' to fail on CreateTextFile
  </code></pre>
  
  <p>This has me stumped I have to admit, if anyone can suggest what it might be I would appreciate it. I should add that McAfee was installed on the machine but I've removed that to rule out its inteference (even though Unlocker should nothing else as interfering with the file) but still the error persists.</p>
  
  <p>Thanks</p>