Results 1 to 4 of 4

Thread: [RESOLVED] Open two text files at the same time - error

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Jun 2003
    Posts
    259

    Resolved [RESOLVED] Open two text files at the same time - error

    i opening two different text files at the same time as different names and i get a error saying the file is already open at the second open statement. whats wrong with what Im doing

    Code:
    Dim strPCNameFromTextFile As String
    
    Dim iFile As Long
    Dim FileNumber As Long
    
    Let iFile = FreeFile
    Let FileNumber = FreeFile
    
    
    Open Environ$("Temp") & "\PCList.txt" For Input As #iFile
    
     While Not EOF(iFile)
          Input #iFile, strPCNameFromTextFile
          
            Open Environ$("Temp") & "\SoftwareInstall.bat" For Output As #FileNumber   ' Open file for output.
    
              Print #FileNumber, strPCNameFromTextFile
        
           Close #FileNumber
     Wend
    
    Close #iFile

  2. #2
    PowerPoster
    Join Date
    Jul 2006
    Location
    Maldon, Essex. UK
    Posts
    6,334

    Re: Open two text files at the same time - error

    You need to assign FileNumber after you've opened the first file
    Code:
    Dim strPCNameFromTextFile As String
    
    Dim iFile As Long
    Dim FileNumber As Long
    
    Let iFile = FreeFile
    
    Open Environ$("Temp") & "\PCList.txt" For Input As #iFile
    Let FileNumber = FreeFile
    
    
     While Not EOF(iFile)
          Input #iFile, strPCNameFromTextFile
          
            Open Environ$("Temp") & "\SoftwareInstall.bat" For Output As #FileNumber   ' Open file for output.
    
              Print #FileNumber, strPCNameFromTextFile
        
           Close #FileNumber
     Wend
    
    Close #iFile
    BTW it's a very long time since I've seen 'Let', it's not required.

  3. #3
    PowerPoster
    Join Date
    Jan 2008
    Posts
    11,074

    Re: Open two text files at the same time - error

    I don't get it. Isn't opening and closing the output file inside the loop causing the output data to be lost and only the last input record from the input file is actually printed to the output file?

    I would think that it should be this way:

    Code:
      '
      '
    Open Environ$("Temp") & "\PCList.txt" For Input As #iFile
    Open Environ$("Temp") & "\SoftwareInstall.bat" For Output As #FileNumber   ' Open file for output.
      '
      '
     While Not EOF(iFile)
          Input #iFile, strPCNameFromTextFile
         
           Print #FileNumber, strPCNameFromTextFile
        
     Wend
    
    Close #iFile
    Close #FileNumber
      '
      '
    Last edited by jmsrickland; Apr 25th, 2012 at 06:30 PM.


    Anything I post is an example only and is not intended to be the only solution, the total solution nor the final solution to your request nor do I claim that it is. If you find it useful then it is entirely up to you to make whatever changes necessary you feel are adequate for your purposes.

  4. #4

    Thread Starter
    Hyperactive Member
    Join Date
    Jun 2003
    Posts
    259

    Re: Open two text files at the same time - error

    thanks Doogle, that did the trick. i been using Let for so long i didnt know it wasnt needed. thanks

    jmsrickland, i have already expanded this to a couple different while loops where i keep creating bat files that then get ran and over written all while the first PClist.txt file remains open. so the original concept remains and it works great now that i put that second freefile in the right place.
    Last edited by seanwpb; Apr 25th, 2012 at 06:57 PM.

Posting Permissions

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



Click Here to Expand Forum to Full Width