VBA to extract text from multiple .txt files in subfolders

mintz

Board Regular
Joined
Aug 5, 2015
Messages
129
I'm looking for a VBA macro to do the following:
- given a specific path, search all folders and subfolders and extract text from all files named "contact.txt"
- for each .txt file insert the content into a new row under column A and its path into column B
These solutions the closest to what I'm trying to achieve
 
According to both attachments :​
  • Demo1 procedure does not need anymore the Charset codeline, delete it …

  • ScanFolder procedure revamped with UTF-8 by default until any UTF-16 BOM is detected :
VBA Code:
Sub ScanFolder(ByVal FOLD$)
      Const C = "contact.txt"
        Dim H$, oFold As Object
    If Obj(1).FileExists(FOLD & C) Then
            R = R + 1
        With Obj(0)
           .Charset = "windows-1252"
           .Open
           .LoadFromFile FOLD & C
            H = .ReadText(2)
           .Position = 0
           .Charset = IIf(H = "þÿ" Or H = "ÿþ", "unicode", "UTF-8")
            S(R, 1) = .ReadText
           .Close
        End With
            S(R, 2) = FOLD
    End If
        For Each oFold In Obj(1).GetFolder(FOLD).SubFolders:  ScanFolder oFold.Path & "\":  Next
End Sub
 
Upvote 0

Excel Facts

Ambidextrous Undo
Undo last command with Ctrl+Z or Alt+Backspace. If you use the Undo icon in the QAT, open the drop-down arrow to undo up to 100 steps.
According to both attachments :​
  • Demo1 procedure does not need anymore the Charset codeline, delete it …

  • ScanFolder procedure revamped with UTF-8 by default until any UTF-16 BOM is detected
This perfectly fixed nearly 100% of the files
The remaining few errors are with files in ANSI and UTF-16 LE (didn't notice I had these encodings before)
Is there a way to handle these two additional encodings?
 
Upvote 0
  • As I have no issue with your UFT-16 LE text file from your first attachment, see post #17 …

  • As you already have marked this thread as solved and checked a post as the solution,
    for any additional encoding see if files contains a BOM like your UTF-16 LE text file
    in order to proceed the same way I used for both UTF-16 formats …

    Or you can use a specific column to enter the encoding format for a new VBA procedure …

    Or via NotePad++ you can convert the files to UTF-8 format for example …
 
Last edited:
Upvote 0
  • As I have no issue with your UFT-16 LE text file from your first attachment, see post #17 …

  • As you already have marked this thread as solved and checked a post as the solution,
    for any additional encoding see if files contains a BOM like your UTF-16 LE text file
    in order to proceed the same way I used for both UTF-16 formats …

    Or you can use a specific column to enter the encoding format for a new VBA procedure …

    Or via NotePad++ you can convert the files to UTF-8 format for example …
I was able to finally resolve this with simply converting the files to UTF-8BOM with batch processing script in PowerShell
The solutions both from John_w and yours worked out perfectly when working with just a single encoding for all files
Thank you very much for your help with this
 
Upvote 0

Forum statistics

Threads
1,214,649
Messages
6,120,732
Members
448,987
Latest member
marion_davis

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