Excel命令CMD列出目录内容

li9yvcax  于 2023-10-22  发布在  其他
关注(0)|答案(2)|浏览(107)

如何修改此Excel文件以:
1.列出目录的路径,而不仅仅是文件?
1.过滤掉系统和隐藏文件?
以下是目前的情况:

Sub MM()
    Dim fResults As Variant
    fResults = GetFiles("C:\Temp")
    Range("A1").Resize(UBound(fResults) + 1, 1).Value = _ 
    WorksheetFunction.Transpose(fResults)
End Sub

// UDF to populate array with files, assign to a Variant variable.
Function GetFiles(parentFolder As String) As Variant
    GetFiles = Filter(Split(CreateObject("WScript.Shell").Exec("CMD /C DIR """ & parentFolder & _
        IIf(Right(parentFolder, 1) = "\", vbNullString, "\") & "*.*"" /S /B /A:-D").StdOut.ReadAll, vbCrLf), ".")
End Function
e4yzc0pl

e4yzc0pl1#

您的Filter ... IIF ...构造也在删除目录。摆脱它.事

GetFiles = Split(CreateObject("WScript.Shell").Exec("cmd /c dir """ & parentFolder & """ /A-H-S /B /S").StdOut.ReadAll, vbNewLine)

最好做你想做的事

fdbelqdn

fdbelqdn2#

答案是更改dir命令的参数。
用途:

DIR /S /B /A-H-S

当前指定的-D隐藏目录。dir参数的完整解释可以在这里找到https://learn.microsoft.com/en-us/windows-server/administration/windows-commands/dir

相关问题