Results 1 to 5 of 5

Thread: Open within an open

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Apr 2005
    Posts
    248

    Open within an open

    Hi all,

    Is it possible to get VB to open a file within another "open"?
    Basically, i'm trying to do this:

    Code:
    Dim FF as integer
    Dim FFF as integer
    Dim NewVal as string
    
    FF = FreeFile()
    FFF = FreeFile()
    
    Open "C:\text.txt" for input as #FF
    While not EOF(FF)
        Input #FF, NewVal
    
        Open "C:\Testoutput.txt" for output as #FFF
        Write #FFF, NewVal
        Close #FFF
    Wend
    Close #FF
    What's happening is that I'm trying to download a file, open it, replace one line, and save it to a different file (this tequnique was taught to me here, for editing files).

    The problem is that if I put that open script within the first one, I get a "File already open" error. Other problems I'm having are (the file I'm trying to open is a webpage downloaded via the internet BTW) that I'm getting some layout / syntax errors.. Maybe it's due to there being " marks in the htm file that was downloaded? Is there any way to get allow it to read these as " marks and not operators for the system?

    Also, the find-replace script that I'm using:

    Code:
    SnipVal = Mid(NewVal, 1, 55)
    '    If SnipVal = "Body {background-color: #000000; background-image: url(" Then
    '        NewVal = "Body {background-color: #000000;"
    '    End If
    Basically I'm trying to remove the background from the webpage. The problem is that there are lots of other commands after that one, and this command is removing all of them.. I've tried lots of different things but is there any way to just pick a string, then a changable variable, then a string? I.E.

    Code:
    If SnipVal = "Body {background-color: #000000; background-image: url(<VariableHereToCoverAnyTextLength>) *more HTML commands here* then
        Dostuff
    end if
    Sorry if that seemed confusing.. I'm hoping that someone can help me on one or more issues

    Thanks ^_^

  2. #2
    PowerPoster Static's Avatar
    Join Date
    Oct 2000
    Location
    Rochester, NY
    Posts
    9,390

    Re: Open within an open

    First part:
    I changed a few things
    VB Code:
    1. Private Sub Form_Load()
    2.     Dim FF As Integer
    3.     Dim FFF As Integer
    4.     Dim NewVal As String
    5.    
    6.     FF = FreeFile
    7.  
    8.     Open "C:\text.txt" For Input As #FF
    9.         Do While Not EOF(FF)
    10.             Input #FF, NewVal
    11.             FFF = FreeFile
    12.             Open "C:\Testoutput.txt" For Append As #FFF
    13.                 Print #FFF, NewVal
    14.             Close #FFF
    15.         Loop
    16.     Close #FF
    17. End Sub
    I am assuming both ff and fff were being set to the same number...
    so i moved it inside the first open so freefile can then see whats being used
    JPnyc rocks!! (Just ask him!)
    If u have your answer please go to the thread tools and click "Mark Thread Resolved"

  3. #3
    PowerPoster Static's Avatar
    Join Date
    Oct 2000
    Location
    Rochester, NY
    Posts
    9,390

    Re: Open within an open

    it seems to me like u just want to change one part of one line??

    Remove the background image part...right? so..
    this should work
    VB Code:
    1. Dim Lines() As String
    2.     Dim tmp As String
    3.     Dim loc As Integer
    4.     Dim x As Integer
    5.     Open "C:\text.txt" For Input As #1
    6.         Lines = Split(Input(LOF(1), 1), vbCrLf)
    7.     Close #1
    8.     Open "C:\text.txt" For Output As #1
    9.         For x = 0 To UBound(Lines)
    10.             loc = InStr(Lines(x), "background-image: url(")
    11.             If loc <> 0 Then
    12.                 loc = loc - 1
    13.                 Lines(x) = Left(Lines(x), loc) & Right(Lines(x), Len(Lines(x)) - InStr(loc + 1, Lines(x), ";"))
    14.             End If
    15.             Print #1, Lines(x)
    16.         Next
    17.     Close #1
    JPnyc rocks!! (Just ask him!)
    If u have your answer please go to the thread tools and click "Mark Thread Resolved"

  4. #4
    I'm about to be a PowerPoster!
    Join Date
    Jan 2005
    Location
    Everywhere
    Posts
    13,647

    Re: Open within an open

    There's nothing wrong with opening multiple files concurrently.

    Static - remember, Open and Close do not form a construct (like Open...Close). That means you can put them wherever you want, and you do not need to nest code within an Open...Close block

    Arby, the reason you get the "File already open" error is because you call Freefile() twice in succession without opening any more files in between. That means that FF and FFF will have the same value. What you can do is call FF=FreeFile(), open the first file, then call FFF=FreeFile() to get the next available file handle.

    VB Code:
    1. Dim FF as Long
    2. Dim FFF as Long
    3. Dim NewVal As String
    4.  
    5. FF = FreeFile()
    6.  
    7. Open "C:\text.txt" For Input as #FF
    8.  
    9. While (Not EOF(FF))
    10.     Input #FF, NewVal
    11.    
    12.     FFF = FreeFile()
    13.     Open "C:\Testoutput.txt" For Output As #FFF
    14.     Write #FFF, NewVal
    15.     Close #FFF
    16. Wend
    17. Close #FF

  5. #5

    Thread Starter
    Addicted Member
    Join Date
    Apr 2005
    Posts
    248

    Re: Open within an open

    Ah, that works well, thank you

    My only problem is now, that it seems to be snipping characters out of the file, and I'm getting LOADS of errors, mostly saying that a ( is missing


    Basically one of those every line.. Any ideas why it might be doing this? My code:

    Code:
    Private Sub Form_Load()
    Dim FF As Integer
    Dim FFF As Integer
    Dim NLT As String
    Dim SnipVal As String
    FF = FreeFile()
    
    
    If Dir("C:\ViewB", vbDirectory) = vbNullString Then
        MkDir "C:\ViewB"
    End If
    
    DownLoadFromURL "http://www.URL.com/", "FILE.TXT"
    
    Open "C:\ViewB\FILE.TXT" For Input As #FF
    While Not EOF(FF)
        Input #FF, NLT
        
        SnipVal = Mid(NLT, 1, 55)
    '    If SnipVal = "Body {background-color: #000000; background-image: url(" Then
    '        NLT = "Body {background-color: #000000;"
    '    End If
    
        Text1.Text = Text1.Text & NLT
        FFF = FreeFile()
        Open "C:\ViewB\FILE.htm" For Append As #FFF
        Print #FFF, Text1.Text
        Close #FFF
        
    Wend
    Close #FF
    
    WB.Navigate ("C:\ViewB\FILE.htm")
    End Sub
    
    Private Sub DownLoadFromURL(ByVal url As String, ByVal filename As String)
        '
        '   Download a file from a given URL
        '
        Dim bytes() As Byte
        Dim fnum As Integer
        Dim ftext As String
        '
        ' Download the file and load into byte array
        '
        ftext = url & filename
        bytes() = WebInet.OpenURL(ftext, icByteArray)
    
        fnum = FreeFile
        '
        '   Write the downloaded file to disk
        '
        Open "C:\ViewB\" & filename For Binary Access Write As #fnum
        
        Put #fnum, , bytes()
        Close #fnum
        
    End Sub
    Basically it downloads a web page from the net, opens the file, looks at each line and tries to match it with the background line (I've commented these bits for now, until I can even get the first part working), replaces the line is it is the background one, and then saves the lines to the new file. When it's done, you *should* have an identical file with only one line changed... But whilst the web page itself downloads perfectly and in-tact, the new output file is a mess and contains many errors.

    I also tried putting an & Chr(13) & Chr(10) & between the Text1.Text & NLT, but it just made the code split up more. I also switched between using Write and Print for outputting the file, but no dice. Oh, and as you can see, I was having problems with the write stage before so I was outputting the text to an invisible text box, then at the end writing the text box to file. I have also tried just writing NLT straight to file now that the code is working, but I still get the same errors. It should also be noted that the textbox (I made it visible to check what was going on) has the same screwed up format as the output file, so the problems must be in the read, not the write (As I said, the downloaded webpage displays fine)

    Oh, Static - That's very nice of you, all that code, but being a complete VB newbie I must say that I really don't understand any of it, or how it works or is supposed to be implemented ^^;;

    Currently, even without using the replace code (in other words, purely reading from the existing web-page and writing to a new one) doesn't work properly (there are a lot of errors). Anything I'm doing wrong with my code?

    EDIT:
    I've just been through the code of the newly-created file for a bit.. it's utterly FUBAR. The file is like, 1000 times the size of the original, all the code is lumped together, spaces and other random characters/symbols are missing, and it seems that the same lines are repeated many times. So many times in fact, that whilst the original file size is 65KB, the new file is 47,617KB O_o... Any ideas?

    EDIT 2: I've just tried the program pulling a simple PHP file from my website, instead of this ghastly complex Xanga blog (the page i've been trying to use). Whilst it encountered one error (Unterminated string constant), and still blocked all the text together and screwed up my formatting, it was nothing on the scale as the page i've been trying to use, and both the downloaded files were the same size (7KB).

    In short, what I'm wondering is, maybe is VB interpreting some of the symbols, like "s and ()s in the text (the HTML) as commands and going back to the start of the script / missing out values / etc. Although that wouldn't account for missing spaces in words...
    Last edited by BubbleLife; Dec 18th, 2005 at 08:12 AM.

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