|
-
Dec 17th, 2005, 10:44 PM
#1
Thread Starter
Addicted Member
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 ^_^
-
Dec 17th, 2005, 10:59 PM
#2
Re: Open within an open
First part:
I changed a few things
VB Code:
Private Sub Form_Load()
Dim FF As Integer
Dim FFF As Integer
Dim NewVal As String
FF = FreeFile
Open "C:\text.txt" For Input As #FF
Do While Not EOF(FF)
Input #FF, NewVal
FFF = FreeFile
Open "C:\Testoutput.txt" For Append As #FFF
Print #FFF, NewVal
Close #FFF
Loop
Close #FF
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"
-
Dec 17th, 2005, 11:14 PM
#3
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:
Dim Lines() As String
Dim tmp As String
Dim loc As Integer
Dim x As Integer
Open "C:\text.txt" For Input As #1
Lines = Split(Input(LOF(1), 1), vbCrLf)
Close #1
Open "C:\text.txt" For Output As #1
For x = 0 To UBound(Lines)
loc = InStr(Lines(x), "background-image: url(")
If loc <> 0 Then
loc = loc - 1
Lines(x) = Left(Lines(x), loc) & Right(Lines(x), Len(Lines(x)) - InStr(loc + 1, Lines(x), ";"))
End If
Print #1, Lines(x)
Next
Close #1
JPnyc rocks!! (Just ask him!)
If u have your answer please go to the thread tools and click "Mark Thread Resolved"
-
Dec 18th, 2005, 02:32 AM
#4
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:
Dim FF as Long
Dim FFF as Long
Dim NewVal As String
FF = FreeFile()
Open "C:\text.txt" For Input as #FF
While (Not EOF(FF))
Input #FF, NewVal
FFF = FreeFile()
Open "C:\Testoutput.txt" For Output As #FFF
Write #FFF, NewVal
Close #FFF
Wend
Close #FF
-
Dec 18th, 2005, 07:37 AM
#5
Thread Starter
Addicted Member
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|