|
-
Sep 27th, 2006, 10:12 PM
#1
Thread Starter
Member
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
-
Sep 28th, 2006, 12:31 AM
#2
Lively Member
Re: Transfer text file content
Try this code, add reference for Microsoft Scripting Runtime library in Project>References
VB Code:
Dim strTemp$
Dim objFSO as Scripting.FileSystemObject, objSourceFile as Scripting.TextStream,objOutputFile as Scripting.TextStream
Set objFSO=New Scripting.FileSystemObject
Set objSourceFile=objFSO.OpenTextFile("source.txt")
Set objOutputFile=objFSO.OpenTextFile("output.txt",ForAppending)
Do While Not objSourceFile.AtEndOfStream
strTemp=objSourceFile.ReadLine
objOutputFile.WriteLine strTemp
Loop
objOutputFile.Close
objSourceFile.Close
Set objOutputFile=Nothing
Set objSourceFile=Nothing
Set objFSO=Nothing
-
Sep 28th, 2006, 12:53 AM
#3
Addicted Member
Re: Transfer text file content
P.S. Sorry for my poor English...
-
Sep 28th, 2006, 03:39 AM
#4
Re: Transfer text file content
VB Code:
Dim FSO As FileSystemObject
Set FSO = New FileSystemObject
FSO.CopyFile "sourceFile", "destinationFile", True 'True for overwrite
-
Sep 28th, 2006, 05:33 AM
#5
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:
Open "a.txt" For Input As 1
atxt = Input(LOF(1), #1)
Close 1
Open "b.txt" For Append As 1
Print #1, atxt
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
-
Sep 28th, 2006, 06:01 AM
#6
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
-
Sep 28th, 2006, 06:07 AM
#7
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|