Extracting text from text files in folder & sub-folders

JARichard74

Board Regular
Joined
Dec 16, 2019
Messages
114
Office Version
  1. 365
Platform
  1. Windows
I am trying to loop through sub-folders, find .txt files, and extract the first line of each file. The following code seems to extract the first 4 lines of the first txt file found. Thanks for any assist.
VBA Code:
Option Explicit

Sub ExtractProponents()
Dim CompanyName As String
Dim i As Integer, fileNumber As Long
Dim xFSO As Object, xFolder As Object, xFile As Object, xSFolder As Object
Dim MyPath As String
Dim objShell As Object, objFolder As Object

Application.ScreenUpdating = False
Application.AutoCorrect.AutoFillFormulasInLists = False

Set objShell = CreateObject("Shell.Application")
Set objFolder = objShell.BrowseForFolder(0, "", 0, 0)
If Not objFolder Is Nothing Then
    MyPath = objFolder.self.Path & "\"
Else
    Exit Sub
    MyPath = "Desktop"
End If

    Set xFSO = CreateObject("Scripting.FileSystemObject")
    Set xFolder = xFSO.GetFolder(MyPath)
    For Each xSFolder In xFolder.subfolders
        For Each xFile In xSFolder.Files
            If xFSO.GetExtensionName(xFile.Path) = "txt" Then
            fileNumber = FreeFile
            Open xFile For Input As #fileNumber
            Line Input #1, CompanyName
            i = i + 1
            Cells(i, "A").Value = CompanyName
            End If
        Next
    Next
    Set xFSO = Nothing
    Set xFolder = Nothing
    Set objFolder = Nothing
    Set objShell = Nothing

End Sub
 

Excel Facts

Save Often
If you start asking yourself if now is a good time to save your Excel workbook, the answer is Yes
Hello JARichard74,
If you still not saw the error I can notice that you not close file after opening.
Your first file stays opened and code reading only first file.
To resolve that, you can add "Close" line and make code look like this.
VBA Code:
                Open xFile For Input As #fileNumber
                Line Input #1, CompanyName
                i = i + 1
                Cells(i, "A").Value = CompanyName
                Close
            End If
        Next
 
Upvote 0
Solution
Thank you EXCEL MAX, as a newb to VBA, it would have taken me a while to figure it out. Marked as solution
Hello JARichard74,
If you still not saw the error I can notice that you not close file after opening.
Your first file stays opened and code reading only first file.
To resolve that, you can add "Close" line and make code look like this.
VBA Code:
                Open xFile For Input As #fileNumber
                Line Input #1, CompanyName
                i = i + 1
                Cells(i, "A").Value = CompanyName
                Close
            End If
        Next
 
Upvote 0
Sometimes when you are tired take a pause and make a breath,
otherwise your mind will be confused.
Thanks to you too.
 
Upvote 0

Forum statistics

Threads
1,215,219
Messages
6,123,688
Members
449,117
Latest member
Aaagu

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top