본문으로 바로가기

Excel VBA 지정한 폴더 안에있는 파일명 출력하는 예제



Sub Main()

    dirName = "c:\practice\"

    Call printFileList(dirName)


End Sub


Sub printFileList(folderName)

    Dim objFSO As Object

    Dim objFolder As Object

    Dim objFile As Object

    Dim i As Integer

    

    'Create an instance of the FileSystemObject

    Set objFSO = CreateObject("Scripting.FileSystemObject")

    'Get the folder object

    Set objFolder = objFSO.GetFolder(folderName)

    i = 1

    'loops through each file in the directory and prints their names and path

    For Each objFile In objFolder.Files

        'print file name

        Cells(i + 1, 1) = objFile.Name

        'print file path

        Cells(i + 1, 2) = objFile.Path

        i = i + 1

    Next objFile


End Sub





이렇게 출력된다.


end.





댓글을 달아 주세요