用于以字节为单位返回文件大小的 VBA Excel 函数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15883237/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 15:20:29  来源:igfitidea点击:

VBA Excel Function for returning file size in byte

vbaexcel-vbafunctionsizebyte

提问by xmux

I wish to return the file size of some files in the same folder or in a different one with VBA in Excel 2010.

我希望在 Excel 2010 中使用 VBA 返回同一文件夹或不同文件夹中某些文件的文件大小。

回答by ZygD

There is a very nice and simple VBA function, which was not mentioned so far, FileLen:

有一个非常漂亮和简单的 VBA 函数,目前还没有提到,FileLen

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

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

It returns the size of the file in bytes.

它以字节为单位返回文件的大小。

In combination with looping through files in a directory it's possible to achieve what you originally wanted (get sizes of files in a folder).

结合循环遍历目录中的文件,可以实现您最初想要的(获取文件夹中文件的大小)。

回答by xmux

Here how to use it in Excel Cell:

以下是如何在 Excel Cell 中使用它:

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

If you have a german Windows than:

如果您使用的是德国 Windows,则:

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

Here is the function for the VBA modul: (Just enable the Developer tools, and copy and paste this into a new modul)

这是 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 ***

回答by user9787014

This is in reply to the question regarding the use of the semicolon ;as a parameter seperator.

这是对有关使用分号;作为参数分隔符的问题的答复。

In locals (like US) which use the point/fullstop .as a decimal point, the coma ,is used as a list seperator, in this case as a seperator in the list of parameters to VBA functions and procedures.

在使用点/句号.作为小数点的本地人(如美国)中,逗号用作,列表分隔符,在这种情况下用作 VBA 函数和过程的参数列表中的分隔符。

In other locals (like DE and Europe in general), where the decimal point is the coma ,things would get confusing if the same symbol was used as a list seperator, so the semcolon ;is being used instead

在其他地方(如 DE 和欧洲一般),小数点是昏迷,,如果使用相同的符号作为列表分隔符,事情会变得混乱,所以使用分号;代替