CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 1 of 2 12 LastLast
Results 1 to 15 of 18
  1. #1
    Join Date
    Oct 2004
    Posts
    19

    search,read,write & replace in a textfile

    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

  2. #2
    Join Date
    Jun 2002
    Location
    West Virginia
    Posts
    131

    Re: search,read,write & replace in a textfile

    Try this:
    Dim NewText As String
    NewText = "New Text" & vbCrLf
    Text1.Text = Left$(Text1.Text, Text1.SelStart) & NewText & Mid$(Text1.Text, Text1.SelStart + 1)
    Wayne

  3. #3
    Join Date
    Oct 2004
    Posts
    19

    Re: search,read,write & replace in a textfile

    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

  4. #4
    Join Date
    Jun 2002
    Location
    West Virginia
    Posts
    131

    Re: search,read,write & replace in a textfile

    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

  5. #5
    Join Date
    Oct 2003
    Location
    Timisoara, Romania
    Posts
    460

    Re: search,read,write & replace in a textfile

    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!

    There is no 'patch' for stupidity.

  6. #6
    Join Date
    Oct 2004
    Posts
    19

    Re: search,read,write & replace in a textfile

    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

  7. #7
    Join Date
    Dec 2001
    Posts
    6,332

    Re: search,read,write & replace in a textfile

    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
    This post may also be of interest to you:
    http://www.codeguru.com/forum/showth...537#post975537
    Please remember to rate the posts and threads that you find useful.
    How can something be both new and improved at the same time?

  8. #8
    Join Date
    Oct 2004
    Posts
    19

    Re: search,read,write & replace in a textfile

    WizBang,

    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
    Attached Files Attached Files
    Last edited by csuryas; November 1st, 2004 at 03:47 AM.

  9. #9
    Join Date
    Jun 2002
    Location
    West Virginia
    Posts
    131

    Re: search,read,write & replace in a textfile

    In this line of code take out one of the commas in your print statement. You have
    Code:
    Open App.Path & "\Test.txt" For Output As #1
        Print #1, , A
    Close #1
    should be
    Code:
    Open App.Path & "\Test.txt" For Output As #1
        Print #1, A
    Close #1
    Wayne

  10. #10
    Join Date
    Oct 2004
    Posts
    19

    Re: search,read,write & replace in a textfile

    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

  11. #11
    Join Date
    Jun 2002
    Location
    West Virginia
    Posts
    131

    Re: search,read,write & replace in a textfile

    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
    Code:
    print #1,"abc"     =       abc
    print #1,,"abc"                =                abc
    print #1,,,"abc"               =                         abc
    If you were saving two variables on one line:
    Code:
    print #1,"abc","def"        =      abc        def
    You can also use a <;> which puts variables side by side with no space
    Code:
    print #1, "abc";"def"          =         abcdef
    Hope this clears it up some,
    Wayne

    Note: The code lined up better in the message box than here.
    Last edited by WayneS; November 1st, 2004 at 09:31 PM.

  12. #12
    Join Date
    Oct 2004
    Posts
    19

    Re: search,read,write & replace in a textfile

    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.

  13. #13
    Join Date
    Jun 2002
    Location
    West Virginia
    Posts
    131

    Re: search,read,write & replace in a textfile

    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.

    Wayne

  14. #14
    Join Date
    Oct 2004
    Posts
    19

    Talking Re: search,read,write & replace in a textfile

    Hi Wayne,
    Thanks alot. it's work but i use only len(a)-1 because i want add 1 crlf at the end of file thanks alot wayne

  15. #15
    Join Date
    Jun 2002
    Location
    West Virginia
    Posts
    131

    Re: search,read,write & replace in a textfile

    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

Page 1 of 2 12 LastLast

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

Featured