File staying open after script executes-VBForums
Results 1 to 7 of 7

Thread: File staying open after script executes

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    Oct 2003
    Location
    Portville NY
    Posts
    780

    File staying open after script executes

    I have a script that will fire once an hour. In this script txt files are created. The next time the script fires, it may or may not create another file (with the same name). However I am getting an permission denied error. I believe this to be because the original file is still open even though the script has finished executing. I can't figure out why the txt file will still be open for editing when I have closed it and cleared out all of the variables from memory. Any help at all would be appreciated. My code for creating the file is below:

    Code:
    If (InStr(ErrorCode,"already exists!")=0) AND (Len(ErrorCode)>0) AND (InStr(ErrorCode,"Invalid Offer!")=0) Then
    				'WRITING SOAPMessage to Text File so we can see what the error is
    				logfile.writeline OrderID & " - Unknown Error See Order Folder for Details"
    				strOutputFile2="C:/LogFiles/" & Cust & "/OrderFiles/"& OrderID &".txt"
    				'wscript.echo strOutputFile2
    				Dim OrderFSO, OrderFile
    				Set OrderFSO = CreateObject("Scripting.FileSystemObject")
    				Set OrderFile = OrderFSO.CreateTextFile(strOutputFile2, TRUE)
    				OrderFile.writeLine (SOAPMessage)
    				orderFile.writeLine "-----------------------"
    				OrderFile.writeLine "Response: " & xmlhttp.status
    				OrderFile.WriteLine xmlhttp.responseText
    				OrderFile.Close()
    				Set strOutputFile2 = nothing
    				Set OrderFile = Nothing
    				Set OrderFSO = Nothing
    			End If
    		End If
    Can anyone see anything that would keep the txt file open even after the script is done executing?
    "...Men will still say THIS was our finest hour"
    If a tree falls in the woods and no one is there to see it, do all the other trees make fun of it?

  2. #2
    Default Member Bonnie West's Avatar
    Join Date
    Jun 2012
    Location
    InIDE
    Posts
    3,142

    Re: File staying open after script executes

    The issue may not have anything to do with the file being left open. After all, you did call the object's Close method before exiting the script. Have you checked the file's attributes (Read-only, etc.)? Where is the file being saved? In what line of the code are you getting the error?
    On Local Error Resume Next: If Not Empty Is Nothing Then Do While Null: ReDim i(True To False) As Currency: Loop: Else Debug.Assert CCur(CLng(CInt(CBool(False Imp True Xor False Eqv True)))): Stop: On Local Error GoTo 0
    Declare Sub CrashVB Lib "msvbvm60" (Optional DontPassMe As Any)

  3. #3

    Thread Starter
    Fanatic Member
    Join Date
    Oct 2003
    Location
    Portville NY
    Posts
    780

    Re: File staying open after script executes

    I have checked the file folder permissions about 100 times to make sure that is not the problem. The error line is here:

    Set OrderFile = OrderFSO.CreateTextFile(strOutputFile2, TRUE)

    If after the script bombs I try to move the file it creates there is an error saying that it can't be moved or modified as it is open by another program.

    I've tried to figure this out and am completely lost.

    Do you think this may have to do with the fact that I am not closing the script in the program? such as: Wscript.end
    "...Men will still say THIS was our finest hour"
    If a tree falls in the woods and no one is there to see it, do all the other trees make fun of it?

  4. #4
    Default Member Bonnie West's Avatar
    Join Date
    Jun 2012
    Location
    InIDE
    Posts
    3,142

    Re: File staying open after script executes

    I don't think WScript.End will have any effect. The Close method and the destruction of the TextStream object should be sufficient.

    If the file is always going to be overwritten, why not delete it first before re-creating it? Also, have you tried the OpenTextFile method?

    BTW, is there any reason for using forward slashes in strOutputFile2?
    Last edited by Bonnie West; Dec 4th, 2012 at 08:35 AM.
    On Local Error Resume Next: If Not Empty Is Nothing Then Do While Null: ReDim i(True To False) As Currency: Loop: Else Debug.Assert CCur(CLng(CInt(CBool(False Imp True Xor False Eqv True)))): Stop: On Local Error GoTo 0
    Declare Sub CrashVB Lib "msvbvm60" (Optional DontPassMe As Any)

  5. #5

    Thread Starter
    Fanatic Member
    Join Date
    Oct 2003
    Location
    Portville NY
    Posts
    780

    Re: File staying open after script executes

    I didn't program all of that so, I can't honestly tell you why there are forward slashes. LOL. I just noticed that as well...

    That probably wouldn't have anything to do with the file permission issue would it? I don't think it would but it doesn't hurt to ask
    "...Men will still say THIS was our finest hour"
    If a tree falls in the woods and no one is there to see it, do all the other trees make fun of it?

  6. #6
    Default Member Bonnie West's Avatar
    Join Date
    Jun 2012
    Location
    InIDE
    Posts
    3,142

    Re: File staying open after script executes

    Quote Originally Posted by Dubya007 View Post
    That probably wouldn't have anything to do with the file permission issue would it?
    I don't know... Have you tried correcting it to backslashes?
    On Local Error Resume Next: If Not Empty Is Nothing Then Do While Null: ReDim i(True To False) As Currency: Loop: Else Debug.Assert CCur(CLng(CInt(CBool(False Imp True Xor False Eqv True)))): Stop: On Local Error GoTo 0
    Declare Sub CrashVB Lib "msvbvm60" (Optional DontPassMe As Any)

  7. #7

    Thread Starter
    Fanatic Member
    Join Date
    Oct 2003
    Location
    Portville NY
    Posts
    780

    Re: File staying open after script executes

    I actually found out what the problem was. We have another program that was monitoring the folders and opening files to read their contents and never closing them. I felt really stupid when I found that out. any way thanks for the help

    Shawn
    "...Men will still say THIS was our finest hour"
    If a tree falls in the woods and no one is there to see it, do all the other trees make fun of it?

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Featured


Click Here to Expand Forum to Full Width

Survey posted by VBForums.