Hi,
i want create a app that can search,read,write and replace the text in textfile. for search and read i have created them. assume that textfile contain
line1
line2
line3
i want to append a text below line2 so the textfile will be like this
line1
line2
somethingelse
line3
can someone help me?
thanks in advance
Last edited by csuryas; October 29th, 2004 at 02:20 AM.
Reason: Have been found the solution for replace
Hi Wayne,
thanks for your reply. but that's still not resolve my problem. all i want is to insert a text in a textfile not in a textbox but thanks for your effort
Sorry about that. I didn't read your post right.
If I get what you want to do is put text in a file that is on the disk.
The only way to do that is create a second file. Read data in and put in second file until you reach your insertion point. Write out the new text to the second file and then continue reading in data from first file and writing to second. Delete the first file and rename the second file to the name of the first file.
This what you need?
Wayne
Hi csuryas,
the best way to handle files is to use the FileSystemObject. You have to add a reference to Microsoft Scripting Runtime from Project References and then use it like this:
for read:
Code:
Dim objFSO as FileSystemObject
Dim objText as TextStream
Dim sText as String
Set objFSO = new FileSystemObject
Set objText = objFSO.OpenTextFile(file, ForReading)
sText = objText.ReadAll
for objText u can use ReadAll for reading all text from the file or you can use a loop and ReadLine to read the file line by line
for writing in a file u can use the same code only this line is changed:
Code:
Set objText = objFSO.OpenTextFile(file, ForWriting)
and then use objText.Write sText
Hope it helps!
You liked it? Please show your gratitude and rate it!
Hi WayneS & vma,
thanks for your reply.
WayneS: i think thats what i need but is there any way to do that without create a new file?
vma: where is the best? using scripting object or use your way? or may be use an open filename for reading as #1?
Thanks for your effort guys
Here is the fastest way I have found to do what you want:
Code:
Dim A$, S$()
'pull content in all at once. care should be taken
'to ensure not to try to read in too large a file
'note that Binary will create the file if it does not exist
Open "C:\test.txt" For Binary Access Read As #1
A = Space$(LOF(1))
Get #1, , A
Close #1
'split contents to an array
S = Split(A, vbCrLf)
'edit contents here
'you can use the Filter() function to filter out certain strings very quickly
'for example to remove any line containing "hello", you can use
'S = Filter(S, "hello", 0, 1)
'join the array to a single string
A = Join(S, vbCrLf)
'you can also open for Binary Access Write and use Put #1, , A, but this method
'does not resize the file if the string is shorter, so you need to use Kill to delete
'the original beforehand, or write to a new file. The use of ";" after the string
'prevents a vbCrLf at the end of the file.
Open "C:\test.txt" For Output As #1
Print #1, A;
Close #1
thanks for your reply. actually your suggestion is work for me but there is something strange with my textfile. everytime i save the textfile there is a tab for the first line. i don't know what's wrong with my coding
i attach the coding can you help me solve my problem?
thanks before
best regards
Last edited by csuryas; November 1st, 2004 at 03:47 AM.
Hi Wayne,
thanks for your reply. it's work good now. but why the result textfile at the end contain 2 crlf? is there any wrong with my coding?
btw i still confuse between print #1, , A and print #1, A
It is a function of the print statement. Whether you are printing to a file or to a form each time you use a <,> in the statement a tab is sent out. However when printing to a file you need one <,> after file number.
ie
Hi Wayne,
thanks for the explanation. uhm one more question why at the end of textfile the program add 1 more vbcrlf? i give the snippets maybe you can help me with these problem
thanks alot for your reply
Code:
For i = 0 To UBound(S)
If S(i) = "[branch]" Then
k = i
Do Until S(k) = ""
k = k + 1
Loop
End If
Next
ReDim T(UBound(S) + 1)
For j = 0 To k - 1
T(j) = S(j)
Next
T(j) = cboBranchName.Text + cboBranchCode.Text
For j = k To UBound(S)
T(j + 1) = S(j)
Next
A = Join(T, vbCrLf)
Open App.Path + "\Test.txt" For Output As #1
Print #1, A;
Close #1
Last edited by csuryas; November 1st, 2004 at 09:46 PM.
lIt's only a guess but the Join is adding a crlf to the end of your string and a crlf is added when saving the file. After your join statement try this.
A=left$(A,Len(A)-2)
that takes out the last crlf. Even though you are printing A with a ; the close statement is more than likely adding a crlf.
crlf is a 2 character set 13 and 10. using len(a)-1 only takes out the line feed but leaves in the carrage return. So use len(a)-2 to remove both the carrage return and the line feed. When you write the file use Print a without the ; and you will get your crlf. I feel that even with the ; the close #1 writes a crlf automaticlly.
Wayne
* The Best Reasons to Target Windows 8
Learn some of the best reasons why you should seriously consider bringing your Android mobile development expertise to bear on the Windows 8 platform.