programing

파일 크기를 바이트 단위로 반환하는 VBA Excel 기능

mytipbox 2023. 6. 27. 23:30
반응형

파일 크기를 바이트 단위로 반환하는 VBA Excel 기능

Excel 2010의 VBA와 같은 폴더 또는 다른 폴더에 있는 일부 파일의 파일 크기를 반환하고 싶습니다.

지금까지 언급하지 않았던 매우 멋지고 간단한 VBA 기능이 있습니다. FileLen:

FileLen("C:\Temp\test file.xls")

파일 크기를 바이트 단위로 반환합니다.

디렉터리에서 파일을 반복 실행하면 원래 원하는 대로(폴더의 파일 크기 가져오기) 할 수 있습니다.

Excel Cell에서 사용하는 방법:

 =GetDirOrFileSize("C:\Users\xxx\Playground\","filename.xxx")

독일어 Windows가 다음보다 많은 경우:

=GetDirOrFileSize("C:\Users\xxx\Playground\";"filename.xxx")

다음은 VBA 모듈의 기능입니다. (개발자 도구를 활성화하고 새 모듈에 복사하여 붙여넣기만 하면 됩니다.)

Function GetDirOrFileSize(strFolder As String, Optional strFile As Variant) As Long

'Call Sequence: GetDirOrFileSize("drive\path"[,"filename.ext"])

   Dim lngFSize As Long, lngDSize As Long
   Dim oFO As Object
   Dim oFD As Object
   Dim OFS As Object

   lngFSize = 0
   Set OFS = CreateObject("Scripting.FileSystemObject")

   If strFolder = "" Then strFolder = ActiveWorkbook.path
   If Right(strFolder, 1) <> "\" Then strFolder = strFolder & "\"
   'Thanks to Jean-Francois Corbett, you can use also OFS.BuildPath(strFolder, strFile)

   If OFS.FolderExists(strFolder) Then
     If Not IsMissing(strFile) Then

       If OFS.FileExists(strFolder & strFile) Then
         Set oFO = OFS.Getfile(strFolder & strFile)
         GetDirOrFileSize = oFO.Size
       End If

       Else
        Set oFD = OFS.GetFolder(strFolder)
        GetDirOrFileSize = oFD.Size
       End If

   End If

End Function   '*** GetDirOrFileSize ***

언급URL : https://stackoverflow.com/questions/15883237/vba-excel-function-for-returning-file-size-in-byte

반응형