 |
ranosb Level: Protégé
 Registered: 10-04-2008 Posts: 5
|
Reading modifing text files
First off I'd like to point out to MODS that registration canno't be completed by inputing the security code, it will say its wrong everytime. U have to leave it blank to register...
NEXT:
Was trying to use Javascript to open windows from links in a text file but found out JS does not read or modify text files.
So I need a project for opening a text file of links, and adding text before and after the links and saving that file or outputting the result into a new text file...
Any helpers out there?
Thanks to all that help!
|
|
10-04-2008 at 04:11 AM |
|
|
ranosb Level: Protégé
 Registered: 10-04-2008 Posts: 5
|
Re: Reading modifing text files
Glad I could be of help,
Now would anybody help me with my question posted?
|
|
10-04-2008 at 07:31 AM |
|
|
admin Level: Administrator

 Registered: 04-04-2002 Posts: 549
|
Re: Reading modifing text files
How is formatted your text file? is it in HTML or a simple list of links one for each line? what text do you want to add after and before the links? should it be a static text for each link? please provide us with as many informations as you can...
with VB6 it is possible to load the entire file in a string then manipulate it and save it back to a text file
An example of reading a file:
Dim sFileText as String
Dim iFileNo as Integer
iFileNo = FreeFile
'open the file for reading
Open "C:Test.txt" For Input As #iFileNo
'change this filename to an existing file! (or run the example below first)
'read the file until we reach the end
Do While Not EOF(iFileNo)
Input #iFileNo, sFileText
'show the text (you will probably want to replace this line as appropriate to your program!)
MsgBox sFileText
Loop
'close the file (if you dont do this, you wont be able to open it again!)
Close #iFileNo
(note: an alternative to Input # is Line Input # , which reads whole lines).
|
An example of writing a file:
Dim sFileText as String
Dim iFileNo as Integer
iFileNo = FreeFile
'open the file for writing
Open "C:Test.txt" For Output As #iFileNo
'please note, if this file already exists it will be overwritten!
'write some example text to the file
Print #iFileNo, "first line of text"
Print #iFileNo, " second line of text"
Print #iFileNo, "" 'blank line
Print #iFileNo, "some more text!"
'close the file (if you dont do this, you wont be able to open it again!)
Close #iFileNo
(note: an alternative to Print # is Write # , which adds commas between items, and also puts the " character around strings).
|
You can read files in a much faster way too, by simply loading the entire file at once. To do this you need to use binary mode, like so:
Function FileText(ByVal filename As String) As String
Dim handle As Integer
' ensure that the file exists
If Len(Dir$(filename)) = 0 Then
Err.Raise 53 ' File not found
End If
' open in binary mode
handle = FreeFile
Open filename$ For Binary As #handle
' read the string and close the file
FileText = Space$(LOF(handle))
Get #handle, , FileText
Close #handle
End Function
|
____________________________
AndreaVB
|
|
10-04-2008 at 08:43 AM |
|
|
ranosb Level: Protégé
 Registered: 10-04-2008 Posts: 5
|
Re: Reading modifing text files
Its a simple links.txt file with links with about 30 links, example;
http://www.andreavb.com
http://www.yahoo.com
http://www.site03.com
http://www.site04.com
http://www.site05.com
http://www.site06.com
I need vb to open the links.txt file, take each line one by one and write the following;
function open1() {
var open1 =
window.open(' http://www.andreavb.com','','scrollbars=yes,height=540,width=785,resizable=yes');
window.open('http://www.yahoo.com','','scrollbars=yes,height=540,width=785,resizable=yes');
window.open('http://www.site03.com','','scrollbars=yes,height=540,width=785,resizable=yes');
window.open('http://www.site04.com','','scrollbars=yes,height=540,width=785,resizable=yes');
window.open('http://www.site05.com','','scrollbars=yes,height=540,width=785,resizable=yes');
Adding the text links one by one 10 at a time then
adding another function, for every 10 links;
}
function open2() {
var open2 =
window.open('http://www.site11.com','','scrollbars=yes,height=540,width=785,resizable=yes');
And when it finishes the last line it adds;
}
And saves all this to a new file
[Edited by ranosb on 10-04-2008 at 09:46 AM GMT]
|
|
10-04-2008 at 09:42 AM |
|
|
admin Level: Administrator

 Registered: 04-04-2002 Posts: 549
|
Re: Reading modifing text files
Here's your function:
Public Sub ReadAndProcessLinkFile(strSourceFile As String, strOutputFile As String)
Dim iSource As Integer
Dim iDest As Integer
Dim strLink As String
Dim iLink As Integer
Dim iFunction As Integer
iSource = FreeFile
Open strSourceFile For Input As #iSource
iDest = FreeFile
Open strOutputFile For Output As #iDest
'read line from source file
iLink = 0
iFunction = 1
Do While Not EOF(iSource)
Input #iSource, strLink
If iLink = 0 Then
'output function header
Print #iDest, "function open" & iFunction & "() {"
Print #iDest, "var open" & iFunction & " ="
End If
Print #iDest, "window.open('" & strLink & "','','scrollbars=yes,height=540,width=785,resizable=yes');"
iLink = iLink + 1
If iLink >= 10 Then
'ouput end function
iFunction = iFunction + 1
iLink = 0
Print #iDest, "}"
End If
Loop
If iLink < 10 Then
Print #iDest, "}"
End If
Close #iDest
Close #iSource
End Sub
|
and this is how to call it, the first parameter is the source text file containing the link lines and the second is the name of the output file you want to generate (if the file exists it will be overwritten!)
Private Sub Command1_Click()
ReadAndProcessLinkFile "c:test.txt", "c:outoput.txt"
End Sub
|
hope it works fine for you!
____________________________
AndreaVB
|
|
10-04-2008 at 12:29 PM |
|
|
ranosb Level: Protégé
 Registered: 10-04-2008 Posts: 5
|
Re: Reading modifing text files
Thank you!
I have VB 6.0 and when I go to help it tells me "The MSDN collection does not exist"
Where can I get this, seems help should be include with VB!
I know there is a different command for this, whats a better way to write this?
If Counter = 1 Then
Print #jFileNo, "function open1() {"
Else
If Counter = 11 Then
Print #jFileNo, ""
Print #jFileNo, "function open2() {"
Else
If Counter = 21 Then
Print #jFileNo, ""
Print #jFileNo, "function open3() {"
End If
End If
End If
[Edited by ranosb on 10-04-2008 at 07:16 PM GMT]
|
|
10-04-2008 at 06:42 PM |
|
|
ranosb Level: Protégé
 Registered: 10-04-2008 Posts: 5
|
Re: Reading modifing text files
Thats the ticket! Thanks Admin...
[Edited by ranosb on 11-04-2008 at 09:53 AM GMT]
|
|
11-04-2008 at 09:53 AM |
|
|
|
|
 |
 |