1
votes

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.

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).

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).

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.

The simplified code is as follows:

Do While objMessages.Count > 0
    Set fsObj = CreateObject("Scripting.FileSystemObject")
    Set tsObj = fsObj.CreateTextFile("C:\Temp\MyFile.txt", True)
    ...
    tsObj.Close
    Set tsObj = Nothing
    Set fsObj = Nothing
    ...
    <some processing which may raise an error>
    ...
    Kill ("C:\Temp\MyFile.txt")    ' Works if no error raised & 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

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.

Thanks

4
How certain are you that an error has not occurred before the code has the chance to close the file (such as an error before the tsObj.Close)?rskar
You have ommitted code at "<some processing which may raise an error>"...any chance that code has openened the file in question, and the error during processing you mention causes you to jump out before it is closed?tcarvin
Could you post the missing section of code please, eventhough you mention it has it's own error handling, it may hold the key ^_^Matt Donnan
You really should be wrapping your call to "Kill" the file in a check for existence. I.e If fsObj.FileExists("C:\Temp\MyFile.txt") Then ...Warren Rox

4 Answers

3
votes

Try the following problem might be the cause of your error:

  1. The program(or other program) is still using the file when you requested it to delete. You must properly close the connection first with the file.

  2. Check first if the assigned path / file exists before creating/deleting the file. If you're sure that the file do exists, try to identify if the program has the right to access the path. You can use the fso.fileexists() or fso.folderexists() to do this.

  3. You might not have permission in accessing system files or path under the other user accounts.

  4. If still not working, check if the file got corrupted. Most likely corrupted file denies the access from your program.

1
votes

I dont think this will solve it all but I think it's worth trying:

Dim fl As File
If fso.FileExists(FileName) Then
    Set fl = fso.GetFile(FileName)
    If (fl.Attributes And ReadOnly) Then
      fl.Attributes = fl.Attributes - ReadOnly
    End If
End If
0
votes

You should probably add a Exit Function or Exit Sub before the errorhandler.

0
votes

I know this is old, but by chance I came across a similar error in some ancient VB6 code I am supporting, and the cause may help someone that lands on this page as I did. The problem turned out to be with the number of file handles opened using FreeFile. If the number returned by FreeFile reaches 255 (which is it's maximum), you start getting a 'Permission Denied' error if you then open another file (which will also have a file handle number of 255) and then try and delete it. In my case, the bug was simply that the files were not being closed after opening.