VBA GetFolder & GetFile (폴더와 파일 속성 가져오기)

Written by

Editorial Team

Reviewed by

Steve Rynearson

Translated by

Younjung Kim

Last updated on 5월 5, 2023

이 튜토리얼에서는 FileSystemObject의 GetFolder 및 GetFile 메서드를 사용하는 방법을 보여 줍니다.

VBA FileSystemObject를 사용하여 폴더 및 파일 속성 가져오기

GetFolder 메서드는 지정된 경로의 폴더에 해당하는 폴더 객체를 반환하고 해당 속성에 액세스할 수 있도록 합니다. GetFile 메서드는 지정된 파일에 대해 동일한 작업을 수행합니다.

VBA 참조 설정하기

먼저 FileSystemObjects를 사용하려면 VB 스크립트 런타임 라이브러리에 대한 참조를 설정해야 합니다. Visual Basic 편집기(ALT+F11)를 열고 드롭다운 메뉴에서 도구 > 참조를 선택한 후 ‘Microsoft Scripting Runtime’의 체크박스를 선택합니다.

FileSystemObject

다음으로, FileSystemObject 객체를 생성해야 합니다:

Dim FSO As New FileSystemObject
Set FSO = CreateObject("Scripting.FileSystemObject")

이제 GetFolder 및 기타 FileSystemObject 메서드에 액세스할 수 있습니다.

GetFolder 메서드 사용하기

우선 액세스하려는 폴더를 지정합니다:

Set fld = FSO.GetFolder("C:\Src\")

아래 코드를 활용하면 폴더를 복사할 수 있습니다:

fld.Copy "C:\NewFolder\"

아래 코드를 활용하면 폴더를 이동할 수도 있습니다:

fld.Move "C:\NewFolder\"

아래 코드를 활용하여 폴더를 삭제할 수도 있습니다:

fld.Delete

아래 코드를 활용하면 새로운 텍스트 파일을 생성할 수도 있습니다:

fld.CreateTextFile “NewTextFile.txt"

이 메서드를 사용하면 폴더의 속성(fld.Attributes), 폴더가 생성된 날짜와 시간(fld.DateCreated), 마지막으로 액세스한 날짜(fld.DateLastAccessed), 마지막으로 수정한 날짜(fld.DateLastModified), 드라이브 이름(fld.Drive), 이름 및 짧은 이름(fld. Name, fld.ShortName), 경로 및 짧은 경로(fld.Path, fld.ShortPath), 크기(fld.Size), 유형(fld.Type), 상위 폴더(fld.ParentFolder), 루트 폴더인지 확인(fld.IsRootFolder) 또는 해당 파일(fld.Files) 또는 하위 폴더(fld.SubFolders) 등에 대해서 반복문을 사용하거나 카운팅할 수 있습니다.

이 모든 것을 프로시저에 넣으면 다음과 같습니다:

Sub FSOGetFolder()
    Dim FSO As New FileSystemObject
    Set FSO = CreateObject("Scripting.FileSystemObject")
    Set fld = FSO.GetFolder("C:\Src\")

    Debug.Print fld.DateCreated
    Debug.Print fld.Drive
    Debug.Print fld.Name
    Debug.Print fld.ParentFolder
    Debug.Print fld.Path
    Debug.Print fld.ShortPath
    Debug.Print fld.Size
    Debug.Print fld.Files.Count
    Debug.Print fld.Type

    For Each fold In fld.SubFolders
        Debug.Print fold.Name
    Next fold

    For Each fil In fld.Files
        Debug.Print fil.Name
    Next fil

End Sub

Ctrl + G를 누르면 직접 실행 창에서 Debug.Print의 결과를 확인할 수 있습니다.

GetParentFolderName 메서드

다음 코드를 사용하여 폴더의 상위 폴더 이름에 액세스할 수도 있습니다:

Dim FSO As New FileSystemObject
Set FSO = CreateObject("Scripting.FileSystemObject") 

ParentFold= FSO.GetParentFolderName("C:\ParentTest\Test\")

예제 코드를 실행하면 ParentFold 는 “C:\ParentTest\” 가 됩니다.

이 메서드는 경로를 확인하지 않으며 지정된 경로의 존재 여부도 확인하지 않는다는 점에 유의하세요.

GetSpecialFolder 메서드

GetSpecialFolder 메서드를 사용하면 인자로 0, 1 또는 2를 전달하여 각각 Windows 폴더 경로(Windows 운영 체제가 설치된 폴더), 시스템 폴더 경로(라이브러리, 폰트 및 장치 드라이버가 있는 폴더) 및 임시 폴더 경로(임시 파일을 저장하는 데 사용되는 폴더)를 가져올 수 있습니다.
With the GetSpecialFolder method, by passing 0, 1 or 2 as an argument, you can get your Windows folder path (with files installed by the Windows operating system), your system folder path (with libraries, fonts, and device drivers) and temporary folder path (the folder that is used for storing temporary files), respectively.

Sub FSOGetSpecialFolder()
    Dim FSO As New FileSystemObject
    Set FSO = CreateObject("Scripting.FileSystemObject")

    Debug.Print FSO.GetSpecialFolder(0) ‘결과값은 C:\Windows 입니다

End Sub

GetFile 메서드

GetFile 메서드는 GetFolder 메서드와 매우 유사한 방식으로 사용할 수 있습니다. 우선 액세스하려는 파일을 지정합니다:

Set fil = FSO.GetFile("C:\Src\Test.xlsx")

위 코드에서 정의한 객체를 사용하여 파일을 복사할 수 있습니다:

fil.Copy "C:\Dst\"

파일을 이동할 수도 있습니다:

fil.Move "C:\Dst\"

파일을을 삭제할 수도 있습니다:

fil.Delete

또는 텍스트 스트림 객체로 파일을 열 수도 있습니다:

fil.OpenAsTextStream

파일의 속성, 파일이 생성된 날짜 및 시간, 마지막으로 액세스하거나 마지막으로 수정한 날짜 및 시간, 드라이브의 문자, 이름 및 짧은 이름, 경로 및 짧은 경로, 크기, 유형 및 상위 폴더와 같은 파일의 속성은 GetFolder 메서드에 설명된 것과 같은 방식으로 액세스할 수 있습니다.

vba-free-addin

VBA 코드 예시 추가 기능

본 웹사이트에 있는 모든 코드 예시에 쉽게 접근해보세요.

메뉴로 이동하여 클릭만 하면 코드는 모듈에 바로 입력됩니다. .xlam 추가 기능.

(설치가 필요 없습니다!)

무료 다운로드

VBA 코드 예시로 돌아가기