hello,
what would be the code that would allow me to read in two text files, concatenate the second one to the end of the first one, then output the file as a new third text file?
thanks in advance,
david
Printable View
hello,
what would be the code that would allow me to read in two text files, concatenate the second one to the end of the first one, then output the file as a new third text file?
thanks in advance,
david
it's pretty simple, you could just search the form. however, here is what would work for you:
if "F:\fileX.txt" (where X is 1 or 2) don't exist, you'll get a run-time error, so you might want to change the path/add error checking.vb Code:
Private Sub Form_Load() Dim File1 As String, File2 As String File1 = StoreFile("F:\file1.txt") File2 = StoreFile("F:\file2.txt") SaveFile "F:\file3.txt", File1 & File2 End Sub Private Function StoreFile(FilePath As String) As String Dim strTemp As String, strReturn As String Open FilePath For Input As #1 Do Until EOF(1) Input #1, strTemp strReturn = strReturn & strTemp & vbCrLf Loop Close #1 StoreFile = strReturn End Function Private Sub SaveFile(FilePath As String, Contents As String) Open FilePath For Output As #1 Print #1, Contents Close #1 End Sub
try this.
create a form with a textbox and a button and put this code.
Private fso As New FileSystemObject
Private strName As String
Private Sub Command1_Click()
readfile1
readfile2
End Sub
Private Sub readfile1()
strName = "c:\file1.txt"
With fso
Set strm = .OpenTextFile(strName, ForReading)
With strm
Do Until .AtEndOfStream
Text1.Text = Text1.Text & .ReadLine
Text1.Text = Text1.Text & .ReadLine
Text1.Text = Text1.Text & .ReadLine
Text1.Text = Text1.Text & .ReadLine
Text1.Text = Text1.Text & .ReadLine
Text1.Text = Text1.Text & .ReadLine
Text1.Text = Text1.Text & .ReadLine
Text1.Text = Text1.Text & .ReadLine
Text1.Text = Text1.Text & .ReadLine
Loop
End With
End With
Exit Sub
End Sub
Private Sub readfile2()
strName = "c:\file2.txt"
With fso
Set strm = .OpenTextFile(strName, ForReading)
With strm
Do Until .AtEndOfStream
Text1.Text = Text1.Text & .ReadLine
Text1.Text = Text1.Text & .ReadLine
Text1.Text = Text1.Text & .ReadLine
Text1.Text = Text1.Text & .ReadLine
Text1.Text = Text1.Text & .ReadLine
Text1.Text = Text1.Text & .ReadLine
Text1.Text = Text1.Text & .ReadLine
Text1.Text = Text1.Text & .ReadLine
Text1.Text = Text1.Text & .ReadLine
Loop
End With
End With
Exit Sub
End Sub
Private Sub createnewfile()
Open App.Path & "\log_erros\log_erros.txt" For Output As #1
Print #1, Text1.TabIndex
Close #1
End Sub
it helped???
This works too. It reads the contents of two files (1.txt and 2.txt) upon a button click. It then creates a new file Results.txt and writes the appended content from both files. All files in code below aer located in the same directory as the application.
Hope it helps!
Code:Private Sub Command1_Click()
Dim FilePath1 As String
Dim FilePath2 As String
Dim OverallContent As String
FilePath1 = App.Path & "\1.txt"
FilePath2 = App.Path & "\2.txt"
OverallContent = ReadFileContents(FilePath1)
OverallContent = OverallContent & ReadFileContents(FilePath2)
OutputResults (OverallContent)
End Sub
'Return file contents
Private Function ReadFileContents(filepath As String) As String
Dim content As String
Open filepath For Input As #1
On Error GoTo errorrk
Input #1, content
errorrk:
Close #1
ReadFileContents = content
End Function
Private Sub OutputResults(OverallContent As String)
On Error GoTo EndSub
Open App.Path & "\Result.txt" For Output As #1
On Error GoTo errorrk
Write #1, OverallContent
errorrk:
Close #1
EndSub:
End Sub
For smaller text files this will work without being slow. For real large ones you might want to use a different method because this will load the entire 2 files into memory then save to a new one. Test it out and see how it performs.
I wrote it kinda quickly so I hope there aren't any bugs.
vb Code:
Option Explicit Private Sub JoinFiles(ByVal File1 As String, ByVal File2 As String, ByVal SaveAs As String) Dim intFF As Integer, strBuffer As String Dim lonL1 As Long, lonL2 As Long lonL1 = FileLen(File1) lonL2 = FileLen(File2) 'Allocate space for the 2 files. strBuffer = Space$(lonL1 + lonL2) intFF = FreeFile Open File1 For Input As #intFF Mid$(strBuffer, 1, lonL1) = Input(LOF(intFF), intFF) Close #intFF intFF = FreeFile Open File2 For Input As #intFF Mid$(strBuffer, lonL1 + 1) = Input(LOF(intFF), intFF) Close #intFF intFF = FreeFile 'Write new file. Open SaveAs For Output As #intFF Print #intFF, strBuffer Close #intFF strBuffer = "" End Sub 'Using the sub. Private Sub Form_Load() 'Joins 1.txt and 2.txt and saves it as NewFile.txt JoinFiles "C:\1.txt", "C:\2.txt", "C:\NewFile.txt" End Sub
or how about the lazy version of DigiRev's code:Code:Private Sub JoinFiles(ByRef File1 As String, ByRef File2 As String, ByRef SaveAs As String)
Dim intFF1 As Integer, intFF2 As Integer, intFF3 As Integer
intFF1 = FreeFile
Open File1 For Input As #intFF1
intFF2 = FreeFile
Open File2 For Input As #intFF2
intFF3 = FreeFile
Open SaveAs For Output As #intFF3
Print #intFF3, Input(LOF(intFF1), #intFF1) & vbCrLf & Input(LOF(intFF2), #intFF2)
Close #intFF1, #intFF2, #intFF3
End Sub
:pQuote:
Originally Posted by bushmobile
Didn't think of doing it like that. That's actually probably a better way to do it.
Another option, the good ol' DOS Copy command.
Code:Private Sub Command1_Click()
Dim strCmd As String
strCmd = "cmd /C copy c:\test1.txt + c:\test2.txt c:\test3.txt /B"
Shell strCmd
End Sub
Here's how I would do it:
DOS might be able to outrun this but not by much.;)Code:Dim TextFile1 As String, TextFile2 As String
Private Sub Form_Load()
Open "FirstFile" For Binary As #1
TextFile1 = Space(LOF(1))
Get 1, , TextFile1
Open "SecondFile" For Binary As #2
TextFile2 = Space(LOF(2))
Get 2, , TextFile2
Open "TargetFile" For Binary As #3
Put 3, , TextFile1 & TextFile2
Close
End Sub
I think digirev's solution is going to work for me, at least it did well in a test run with the largest text files I'll probably be doing with.
thanks,
david