如何在Mac上的给定文件夹中读取文件名?

发布于 2025-01-27 02:51:17 字数 786 浏览 2 评论 0原文

我的目标是阅读给定文件夹中所有PNG文件的文件名。

我使用Windows VBA代码,该代码使用ActiveX FileSystemobject。

在Mac上,此代码导致

“运行时错误429 Activex组件无法创建对象”

Function ReadFileNames(ByVal sPath As String) As Integer
    
    Dim oFSO, oFolder, oFile As Object
    Dim sFileName As String
                        
    Set oFSO = CreateObject("scripting.FileSystemObject")
    Set oFolder = oFSO.getfolder(sPath)
            
    For Each oFile In oFolder.Files
        
        If Not oFile Is Nothing And Right(LCase(oFile.Name), 4) = ".png" Then  ' read only PNG-Files
            sFileName = oFile.Name
            ' do something with the FileName ...
        End If
        
    Next oFile
        
End Function

My goal is to read the FileNames of all png files in a given folder.

I've Windows VBA code which uses the ActiveX FileSystemObject.

On a MAC This code results in

"runtime error 429 activex component can't create object"

Function ReadFileNames(ByVal sPath As String) As Integer
    
    Dim oFSO, oFolder, oFile As Object
    Dim sFileName As String
                        
    Set oFSO = CreateObject("scripting.FileSystemObject")
    Set oFolder = oFSO.getfolder(sPath)
            
    For Each oFile In oFolder.Files
        
        If Not oFile Is Nothing And Right(LCase(oFile.Name), 4) = ".png" Then  ' read only PNG-Files
            sFileName = oFile.Name
            ' do something with the FileName ...
        End If
        
    Next oFile
        
End Function

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

南巷近海 2025-02-03 02:51:17

这是一个使用本机VBA DIR命令的子,通过在调试窗口上打印其名称,将Excel Workbook列出:

Public Sub DirXlList()
    Const cstrPath As String = "c:\users\xxxx\misc\"
    Dim strDirItem As String
    strDirItem = Dir(cstrPath & "*.xlsx")
    While strDirItem <> ""
       Debug.Print "FileName: " & strDirItem, "FullPath: " & cstrPath & strDirItem
       strDirItem = Dir()
       DoEvents
    Wend
End Sub

这有帮助吗?在

更新中:doevents命令允许Excel处理其他未决的用户界面活动,例如窗口刷新,鼠标单击。如果您的文件夹中有很多文件(成千上万个),那么Excel可能会在这样的循环中显得无反应/冻结。这不是必需的,因为一旦完成循环,它将再次响应。如果您只有几百个文件,那么它是一个过度杀伤。删除并尝试。

Here is a sub, using the native VBA DIR command, listing EXCEL workbooks in a folder by printing their names on the debug window:

Public Sub DirXlList()
    Const cstrPath As String = "c:\users\xxxx\misc\"
    Dim strDirItem As String
    strDirItem = Dir(cstrPath & "*.xlsx")
    While strDirItem <> ""
       Debug.Print "FileName: " & strDirItem, "FullPath: " & cstrPath & strDirItem
       strDirItem = Dir()
       DoEvents
    Wend
End Sub

Does this help? In

Update: doevents command allows Excel to process other pending user interface activities, such as window refreshes, mouse-clicks. If you have lots of files (thousands) in a folder, Excel may appear unresponsive/frozen in a loop like this. It is not necessary, as it will become responsive again, once it completes the loop. If you have only a few hundred files then it is an overkill. Remove and try.

没企图 2025-02-03 02:51:17

Mac的VBA可以链接到整个C标准库,例如此示例:

Private Declare PtrSafe Function CopyMemory_byPtr Lib "libc.dylib" Alias "memmove" (ByVal dest As LongPtr, ByVal src As LongPtr, ByVal size As Long) As LongPtr

我懒得为您写相关示例,但是如果您偶然地使用C标准库进行文件操作,则可以那样做。

VBA for Mac can link to the entire c standard library, like this example:

Private Declare PtrSafe Function CopyMemory_byPtr Lib "libc.dylib" Alias "memmove" (ByVal dest As LongPtr, ByVal src As LongPtr, ByVal size As Long) As LongPtr

I'm too lazy to write out relevant examples for you, but if, by chance, you are familiar with using the c standard library for file manipulation, you can just do it that way.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文