Solved

System says Closed file is still open.  Do I use Reset instead of Close?

Posted on 2004-08-21
18
377 Views
Last Modified: 2010-05-02
I am using a sequentail file in one of my VB forms.  I first read the file to extract information.  When I am done I close it.  The exact code I use is:

  FileName = App.Path & filDetectTemp
  OutputFileNo = FreeFile
  Open FileName For Output As #OutputFileNo
'
' Open Input File and Start Reading
'
  FileName = App.Path & filDetect
  InputFileNo = FreeFile
  Open FileName For Input As #InputFileNo
 
  Do While Not EOF(InputFileNo)
    Line Input #InputFileNo, linebuffer
'    linebuffer = Crypt(linebuffer)
    CopyMemory TempDetect, ByVal linebuffer, Len(TempDetect)
    '
    ' when reading through detection always want to bypass level "00" (baselevel) records for
    ' this evaluation.
    ' If it is an upgrade will also want to bypass any upgrades of that level
    '
    '
    If TempDetect.Eval_ID = whichEval Then
        If TempDetect.DOLevel = BaseLevel Or TempDetect.DOLevel = whichLevel Then
        Else
            '
            ' write output record from input to output
            '
        CopyMemory ByVal linebuffer, TempDetect, Len(TempDetect) - 2
        Print #OutputFileNo, linebuffer
        End If
    End If
  Loop
  Close #InputFileNo

The next thing I do in the same program is use the name statement to copy the input file to a backup using:

Name App.Path & filDetect As App.Path & filDetectBack

The system is giving me an error that file is open.  The file it is indicating is App.Path & filDetect.  This logic is immediately after the close.  Why wouldit tell me the file was still open?

I have a book with a 'Tip' that says:  The Reset  command, unlike the Close command seems to force the underlying operating system to flush the buffers.  Use this command in critical sitiations to make sure that the underlying operating system file buffer is flushed.  However it stops there.  I have no sysntax on the Reset command.  Do I use it instead of the close command?  What is the format/syntax?
0
Comment
Question by:mlcktmguy
  • 7
  • 5
  • 2
  • +3
18 Comments
 
LVL 75

Expert Comment

by:Anthony Perkins
ID: 11861045
Change:
Close #InputFileNo

To:
Close #OutputFileNo
0
 
LVL 75

Expert Comment

by:Anthony Perkins
ID: 11861052
Actually that is not correct.  Make the following change:

...
        End If
    End If
  Loop
  Close #InputFileNo
  Close #OutputFileNo

0
 
LVL 85

Expert Comment

by:Mike Tomlinson
ID: 11861280
If you issue the Close command without a filenumber then it closes all open files that you have:

    Close ' Make sure everything is closed!

Idle_Mind
0
 
LVL 1

Author Comment

by:mlcktmguy
ID: 11861391
Thanks for the input but this doesn't resolve my situation.  I don't want everything closed.  I want to keep the output file open because I may be adding information to it later.

'acperkins', Input file is the one that I am closing in my current code.  it is also the one which I later get an error on in the 'Name' command.    That is the part that I don't understand.  The system says it is still open.
0
 
LVL 7

Expert Comment

by:petoskey-001
ID: 11861960
In regards to the reset command you asked for, here is the syntax from MSDN...
-----------------------
Closes all disk files opened using the Open statement.

Syntax

Reset

Remarks

The Reset statement closes all active files opened by the Open statement and writes the contents of all file buffers to disk.
-------------------------
Since you don't want to use Close, reset it probably not what you want.

If the system says it's still open, it's probably right.  First find out what file the system is actually trying to rename using a debug.print statement right before you name statement.

debug.print "About to run 'Name " & App.Path & filDetect As App.Path & filDetectBack & "' statement"
Name App.Path & filDetect As App.Path & filDetectBack

You may find your not working with the file you think you are.

Next, figure out what programs have the file open.  The system is always right.  So download "Process Explorer" from http://sysinternals.com/  Search for the filename and it will show you every process on your system that is using that file.  It will also let you kill it with a simple click.  

Another possibility that you should consider is that your program is running out of the order you guessed.  If you trace through the program, maybe your function is being called more then once or recursively, so your program has the same file open and is trying to rename it while it still has it open.  Just guessing here, but that's what I would check.
0
 
LVL 3

Expert Comment

by:PocketLintPPC
ID: 11862067
Check filDetectBack to see if it is still open someplace, It looks like you are correctly closing InputFileNo(filDetect) so I'm guseeing that filDetectBack is still open somepalce.

Hope that helps some
0
 
LVL 85

Expert Comment

by:Mike Tomlinson
ID: 11862326
The system may actually still be writing to the file after you close it and then try to rename it.  You could use this type of structure to trap the error and delay for a second before retrying again:

Private Sub foo()
    Dim delayTime As Date
    Dim tries As Integer

    ' more code above...
    Do While Not EOF(InputFileNo)
        ' abbreviated code...
    Loop
    Close #InputFileNo
   
    On Error GoTo delayAndTryAgain
rename:
    Name App.Path & filDetect As App.Path & filDetectBack
    Exit Sub
   
delayAndTryAgain:
    tries = tries + 1
    If tries = 6 Then
        MsgBox "Unable to rename file", vbCritical, "Rename Error"
        Exit Sub
    End If
    delayTime = DateAdd("s", 1, Now())
    While Now() < delayTime
        DoEvents
    Wend
    Resume rename
End Sub
0
 
LVL 7

Expert Comment

by:Enlade
ID: 11864379

I would like to know if you have any On Error statements in you code already?  Maybe you are hitting an error and it is jumping past the Close statement on your input file.  Just a quick throught that you should be able to rule out quickly if you don't use any On Error statements.
0
 
LVL 7

Expert Comment

by:Enlade
ID: 11864410

For instance, to give you an idea of how the On Error could be a problem.  Lets say your function looks something like this:

Public Sub Blah()

  On Error Goto errBlah

  FileName = App.Path & filDetectTemp
  OutputFileNo = FreeFile

   ...
   ...
   ...

  Loop
  Close #InputFileNo
  Exit Sub

errBlah:

End Sub

And then lets say that on return from this function (in some other calling function) you try to copy the input file to your backup location.  Now, if something in this function gives you an error (like the Copy memory or whatever) then the file will never be closed and you will get the error.

Keep in mind that the On Error doesn't have to be in this function to be causeing a problem for you.  You could have the On Error in one of the calling functions and then it will jump all the way back out when it hits the error.  So, I was just wondering if you are using any On Error's in your code?


0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 1

Author Comment

by:mlcktmguy
ID: 11864518
no On Error statement currently
0
 
LVL 7

Expert Comment

by:Enlade
ID: 11864565

You do know that you are openning the output file, but then never closing it?  I see people have said that already and you said that you want to keep it open so you could add more data to it; however, if by "add more data" you mean call this function again then that is a problem because you will be reopenning the output file on each call to this function.  You would need to move the open of the output file out of this function and only open it once.  Maybe that is related to the problem you are having.
0
 
LVL 1

Author Comment

by:mlcktmguy
ID: 11864609
Yes I do know that.  I am closing it below.
0
 
LVL 7

Accepted Solution

by:
Enlade earned 125 total points
ID: 11864656

Just to test things.  Try using the Close and then reopenning the output file before and after the copy.  I know you don't want to close the output file, but just try it to see what happens.  In other words, try this code just to test.


Close

MsgBox App.Path & filDetect
MsgBox App.Path & filDetectBack

Name App.Path & filDetect As App.Path & filDetectBack

FileName = App.Path & filDetectTemp
Open FileName For Output As #OutputFileNo


Tell us what the MsgBox's print and also what error you get?
0
 
LVL 1

Author Comment

by:mlcktmguy
ID: 11864735
The message boxes show me the paths and names of the files.  I get an error on:

Name App.Path & filDetect As App.Path & filDetectBack

telling me that the file already exists.
0
 
LVL 7

Expert Comment

by:Enlade
ID: 11864899

Can you tell me exactly what you see in the message boxes?

0
 
LVL 7

Expert Comment

by:Enlade
ID: 11864969

There are three things we need to confirm.

First, that "App.Path & filDetect" and "App.Path & filDetectBack" are both filenames.
Second, that"App.Path & filDetectBack" does not already exist.
Thrid, that the full path contained in "App.Path & filDetectBack" already exists.

So, if you tell me what you see in the message boxes it will help me to confirm the above with you.  Then we can continue with some other tests.


0
 
LVL 7

Expert Comment

by:Enlade
ID: 11865009

Basically you should be constructing the path and the filename in seperate strings.  Then you can check to see if the TO path exists and if it does not already exist you can create the path.  Then you need to check to see if the TO path\filename exists.  If it already exists then you need to decide what you want to do (delete it, use a different backup path\filename, ask the user, etc).  In any case, before you continue you need to make sure that no TO path\filename exists.  At which time, you can then use the Name command to copy the file over to the TO path\filename.

I can give you all those functions if you want (like File_Exists() and Path_Exists() and such).  But for now lets just try to figure out why it thinks the TO file exists.
0
 
LVL 1

Author Comment

by:mlcktmguy
ID: 11880943
I have tried this many times since your last response.  I cannot duplicate the original error that I was getting, which told me the file was still open.  I will resubmit the question to EE when i can pin it down to something more specific.  In the meantime I am awarding the points to Enlade for all of the time and helpful suggestions supplied.
0

Featured Post

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

Introduction In a recent article (http://www.experts-exchange.com/A_7811-A-Better-Concatenate-Function.html) for the Excel community, I showed an improved version of the Excel Concatenate() function.  While writing that article I realized that no o…
This article describes some techniques which will make your VBA or Visual Basic Classic code easier to understand and maintain, whether by you, your replacement, or another Experts-Exchange expert.
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

746 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now