|
|
Question : Consolidating .txt files
|
|
I've got about 1000 .txt files that I need to consolidate for sorting and searching purposes. I'm not looking forward to a lot of cut and paste. Can Word or Excel import them all at once? They're all fairly small documents and similar in format, I just need them all on one huge page so I can play with them. (I'd like to avoid Access, I don't need to do complicated stuff afterwards)
|
Answer : Consolidating .txt files
|
|
Try the following sub. It will append all of the .txt files in a directory onto the active sheet. I didn't bother to parse the text (each line of the text file goes into the first cell of a row), but this could be done easily.
Sub AddText() 'Appends contents of all text files in a directory to the active sheet Dim i As Integer, NewRw As Integer Dim f, fs, nextLine, ts Dim nextFile As String, Path As String Const ForReading = 1, ForWriting = 2, ForAppending = 3 NewRw = ActiveSheet.UsedRange.Rows.Count Path = InputBox("Please enter the desired path") If Right(Path, 1) <> "\" Then Path = Path & "\" nextFile = Dir(Path & "*.txt") If nextFile = "" Then Exit Sub Set fs = CreateObject("Scripting.FileSystemObject")
For i = 1 To 10000 Set f = fs.OpenTextFile(Path & nextFile, ForReading) If IsEmpty(f) Then Exit For Do While f.AtEndOfStream <> True nextLine = f.ReadLine NewRw = NewRw + 1 ActiveSheet.Cells(NewRw, 1) = nextLine Loop f.Close nextFile = Dir If nextFile = "" Then Exit For Next i End Sub
|
|
|
|
|