Results 1 to 7 of 7

Thread: Transfer text file content

  1. #1

    Thread Starter
    Member
    Join Date
    Sep 2006
    Posts
    54

    Transfer text file content

    Hi everyone, can someone show me a code how to transfer content of a text file to another text file?

    one text file named A.text i want to open using "For Open" and another one (B.text) using "For Append"..so i want everything in A.text transfer to B.text

  2. #2
    Lively Member okosv's Avatar
    Join Date
    Sep 2006
    Posts
    95

    Re: Transfer text file content

    Try this code, add reference for Microsoft Scripting Runtime library in Project>References
    VB Code:
    1. Dim strTemp$
    2. Dim objFSO as Scripting.FileSystemObject, objSourceFile as Scripting.TextStream,objOutputFile as Scripting.TextStream
    3. Set objFSO=New Scripting.FileSystemObject
    4. Set objSourceFile=objFSO.OpenTextFile("source.txt")
    5. Set objOutputFile=objFSO.OpenTextFile("output.txt",ForAppending)
    6. Do While Not objSourceFile.AtEndOfStream
    7.      strTemp=objSourceFile.ReadLine
    8.      objOutputFile.WriteLine strTemp
    9. Loop
    10. objOutputFile.Close
    11. objSourceFile.Close
    12. Set objOutputFile=Nothing
    13. Set objSourceFile=Nothing
    14. Set objFSO=Nothing

  3. #3
    Addicted Member
    Join Date
    Mar 2002
    Location
    Lithuania
    Posts
    165

    Re: Transfer text file content

    cant u just copy file?
    P.S. Sorry for my poor English...

  4. #4
    PowerPoster gavio's Avatar
    Join Date
    Feb 2006
    Location
    GMT+1
    Posts
    4,462

    Re: Transfer text file content

    VB Code:
    1. Dim FSO As FileSystemObject
    2.     Set FSO = New FileSystemObject
    3.         FSO.CopyFile "sourceFile", "destinationFile", True 'True for overwrite

  5. #5
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: Transfer text file content

    this is how to do it if you don't want to use the file system object (FSO)
    more or less what you had in your post
    VB Code:
    1. Open "a.txt" For Input As 1
    2.     atxt = Input(LOF(1), #1)
    3.     Close 1
    4. Open "b.txt" For Append As 1
    5.     Print #1, atxt
    6.     Close 1
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

  6. #6
    Shared Member
    Join Date
    May 2005
    Location
    Kashmir, India
    Posts
    2,277

    Re: Transfer text file content

    Why not just FileCopy if all you want to do is copy the contents from one file to another.
    Use [code] source code here[/code] tags when you post source code.

    My Articles

  7. #7
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: Transfer text file content

    from his original post it would appear he wanted to add the content to the second existing file
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

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