Search This Blog

2016/12/21

VBA: Get all files' name and content in folder

As an example, save txt files in C://temp/

Code:
Dim file As String
Dim r As Integer
Dim text As String

Path = "C://temp/"
'get first file name
fileName =Dir(Path + "*.txt")
r = 1
'Start the loop.
Do While Len(fileName) > 0
    'output file name
    Cells(r, 1).Value = fileName
    'get content
    filePath = Path + fileName
    Open filePath For Input As #1
        Do Until EOF(1)
            Line Input #1, text
        Loop
        'output content
        Cells(r, 2).Value = text
    Close #1

    'Get next file.
    file = Dir()
Loop
Reference:
Dir Function:Returns a string representing the name of a file, directory, or folder that matches a specified pattern or file attribute, or the volume label of a drive.

No comments :

Post a Comment