VBA FileSystemObject (FSO) | FileSystemObject에 액세스하는 방법?

Excel VBA FileSystemObject (FSO)

VBA FileSystemObject (FSO) 는 우리가 작업중인 컴퓨터의 다른 파일에 액세스하는 데 사용되는 FileDialog와 유사하게 작동합니다. 이 파일을 편집 할 수도 있다는 것은 파일을 읽거나 쓰는 것을 의미합니다. FSO를 사용하여 파일에 액세스하고 작업하고 파일과 폴더를 수정할 수 있습니다. FSO는 VBA로 액세스 할 수있는 중요한 API 도구입니다. VBA 프로젝트의 일부로 작업을 완료하려면 컴퓨터의 몇 개의 폴더와 파일에 액세스해야 할 수 있습니다.

FSO를 사용하여 "폴더를 사용할 수 있는지 여부 확인", 새 폴더 또는 파일 만들기, 기존 폴더 또는 파일 이름 바꾸기, 폴더의 모든 파일 목록 및 하위 폴더 이름 가져 오기와 같은 많은 작업을 수행 할 수 있습니다. 마지막으로 한 위치에서 다른 위치로 파일을 복사 할 수 있습니다.

폴더 및 파일 작업에 사용할 수있는 다른 기능이 있더라도 FSO는 VBA 코드를 깔끔하고 똑바로 유지하여 폴더 및 파일 작업을 수행하는 가장 쉬운 방법입니다.

FileSystemObject로 4 가지 유형의 객체에 액세스 할 수 있습니다. 다음은 그입니다.

  1. 드라이브 : 이 개체를 사용하여 언급 된 드라이브의 존재 여부를 확인할 수 있으며 경로 이름, 드라이브 유형 및 드라이브 크기를 가져올 수 있습니다.
  2. 폴더 : 이 개체를 통해 특정 폴더의 존재 여부를 확인할 수 있습니다. 이 개체를 사용하여 폴더를 생성, 삭제, 수정, 복사 할 수 있습니다.
  3. 파일 : 이 개체를 사용하면 특정 파일의 존재 여부를 확인할 수 있습니다. 이 vba 객체를 사용하여 파일을 생성, 삭제, 수정, 복사 할 수 있습니다.
  4. 텍스트 스트림 : 이 개체를 사용하면 텍스트 파일을 만들거나 읽을 수 있습니다.

위의 모든 방법에는 고유 한 방법이 있습니다. 우리의 요구 사항에 따라 각 개체의 방법을 선택할 수 있습니다.

FileSystemObject를 활성화하는 방법?

VBA에서는 쉽게 액세스 할 수 없습니다. 파일 및 폴더에 액세스하는 것은 Excel의 외부 작업이므로 FileSystemObject를 활성화해야합니다. 활성화하려면 아래 단계를 따르십시오.

1 단계 : 도구> 참조로 이동합니다.

2 단계 – 'Microsoft Scripting Runtime'옵션 선택

아래로 스크롤하여 'Microsoft Scripting Runtime'옵션을 선택합니다. 옵션을 선택한 후 확인을 클릭하십시오.

이제 vba에서 FileSystemObject (FSO)에 액세스 할 수 있습니다.

FileSystemObject의 인스턴스 만들기

개체 라이브러리에서 'Microsoft Scripting Runtime'옵션이 활성화되면 코딩을 통해 FSO (파일 시스템 개체)의 인스턴스를 만들어야합니다.

인스턴스를 만들려면 먼저 변수를 FileSystemObject 로 선언합니다 .

보시다시피 FileSystemObject 가 VBA의 IntelliSense 목록에 나타납니다. 'Microsoft Scripting Runtime'을 활성화하기 전에는 사용할 수 없었습니다.

FSO는 개체이므로 새 인스턴스를 만들려면 FSO를 설정해야합니다.

이제 FSO (FileSystemObject)의 모든 옵션에 액세스 할 수 있습니다.

VBA FileSystemObject 사용 예제

이 VBA FileSystemObject Excel 템플릿은 여기에서 다운로드 할 수 있습니다. – VBA FileSystemObject Excel 템플릿

예제 # 1 – 전체 드라이브 공간 찾기

아래 코드는 드라이브의 총 공간을 제공합니다.

암호:

 Sub FSO_Example1 () Dim MyFirstFSO As FileSystemObject Set MyFirstFSO = New FileSystemObject Dim DriveName As Drive Dim DriveSpace As Double Set DriveName = MyFirstFSO.GetDrive ( "C :") 'Create new drive object DriveSpace = DriveName.FreeSpace'이것은 여유 공간을 얻습니다. "C"드라이브의 DriveSpace = DriveSpace / 1073741824 '이것은 여유 공간을 GB로 변환합니다. DriveSpace = Round (DriveSpace, 2)'전체 공간을 반올림합니다. MsgBox "Drive"& DriveName & "has"& DriveSpace & "GB"End 보결 

코드 분석.

먼저 FSO의 인스턴스를 만들었습니다.

 Dim MyFirstFSO As FileSystemObject Set MyFirstFSO = New FileSystemObject

Next, we have declared two variables.

 Dim DriveName As Drive Dim DriveSpace As Double 

Since DriveName is an Object variable we need to set this to FSO one of the FSO method. Since we need the characteristic of the drive we have used Get Drive option and mentioned the drive name

 Set DriveName = MyFirstFSO.GetDrive("C:")

Now for another variable DriveSpace, we will assign the free space method of the drive we are accessing.

DriveSpace = DriveName.FreeSpace

As of now, the above equation can get us free space of the drive “C”. So to show the result in GB we have divided the free space by 1073741824

DriveSpace = DriveSpace / 1073741824

Next, we will round the number.

DriveSpace = Round(DriveSpace, 2)

Finally, show the result in Message Box.

MsgBox "Drive " & DriveName & " has " & DriveSpace & "GB"

When we run the code manually or through shortcut key F5, then in message box we will get the free space of the drive “C”.

So, in my computer Drive C has 216.19 GB of free space memory.

Example #2 – Check Whether the Folder Exists or Not

To check whether the particular folder exists or not use the below code.

If the mentioned folder is available then it will show us the message box as “The Mentioned Folder is Available”, if not it will show the VBA message box as “The Mentioned Folder is Not Available”.

Code:

 Sub FSO_Example2() Dim MyFirstFSO As FileSystemObject Set MyFirstFSO = New FileSystemObject If MyFirstFSO.FolderExists("D:\Excel Files\VBA\VBA Files") Then MsgBox "The Mentioned Folder is Available" Else MsgBox "The Mentioned Folder is Not Available" End If End Sub 

Run this code through the excel Shortcut key F5 or manually, then see the result.

Example #3 – Check Whether the File Exists or Not

Below code will check whether the mentioned file is available or not.

Code:

 Sub FSO_Example3() Dim MyFirstFSO As FileSystemObject Set MyFirstFSO = New FileSystemObject If MyFirstFSO.FileExists("D:\Excel Files\VBA\VBA Files\Testing File.xlsm") Then MsgBox "The Mentioned File is Available" Else MsgBox "The Mentioned File is Not Available" End If End Sub 

Run this code manually or using the F5 key, then see the result.