简体   繁体   English

Excel VBA通​​过多个文件扩展名和上次修改日期获取文件列表

[英]Excel VBA to get a file list by multiple file extensions and last modified date

In my folder, I have different types of files: 在我的文件夹中,我有不同类型的文件:

.mp4 .wav .out .outreview .mp4 .wav .out .outreview

I am using a VBA code in excel to make a list of all of the files according to their file extension. 我在excel中使用VBA代码,根据文件扩展名列出所有文件。 As seen in the screenshot: 如屏幕截图所示:

在此处输入图片说明



To do this, I use the following code four times, each time I replace the file extension and adjust the column references: 为此,我每次使用以下代码四次,每次替换文件扩展名并调整列引用时:

this is the example code for the first column, the video files, .mp4: 这是第一列的视频文件.mp4的示例代码:

Sub getfilelistfromfolder()
    Dim varDirectory As Variant
    Dim flag As Boolean
    Dim i As Long
    Dim strDirectory As String
    Dim Desired As String

    strDirectory = Application.ActiveWorkbook.Path & "\"
    i = 1
    flag = True
    varDirectory = Dir("C:\Users\Folder\*.mp4", vbNormal)
    Range("A5:A100").Select
    Selection.ClearContents

    While flag = True
        If varDirectory = "" Then
            flag = False
        Else
            Cells(i + 4, 1) = varDirectory
            varDirectory = Dir
            i = i + 1
        End If
    Wend
End Sub



I repeat it for .wav and .outreview files. 我对.wav和.outreview文件重复此操作。 But I use the following code for .out files, because, otherwise, it pulls up all the .out and . 但是我将以下代码用于.out文件,因为否则,它将拉出所有.out和。 outreview files in the .out column, which I don't want. 我不需要的.out列中的outreview文件。

Sub getfilelistfromfolder()
    Dim varDirectory As Variant
    Dim flag As Boolean
    Dim i As Long
    Dim strDirectory As String
    Dim Desired As String

Desired = ".out"

strDirectory = Application.ActiveWorkbook.Path & "\"
i = 1
flag = True
varDirectory = Dir("C:\Users\Folder\", vbNormal)

While flag = True
    If varDirectory = "" Then
        flag = False
    Else
        If Right(varDirectory, 4) = Desired Then
            Cells(i + 4, 3) = varDirectory
            i = i + 1
        End If
        varDirectory = Dir
    End If
Wend
End Sub



Question: how can I pull the last modified date ONLY for .out files and put them in their corresponding cells in column D ? 问题:如何将.out文件的最后修改日期拉到D列的相应单元格中?

And how can I combine all these codes into one code so that I don't repeat this for each file extension? 以及如何将所有这些代码合并为一个代码,这样我就不会在每个文件扩展名上都重复此操作? Thanks 谢谢



UPDATE : 更新



this update is after the answer provided by Jeanno : 此更新是在Jeanno提供的答案之后:

This answers my question. 这回答了我的问题。 here is the Jeanno's corrected version of the code. 这是Jeanno的更正版代码。



Sub getfilelistfromfolder()
    Dim varDirectory As Variant
    Dim flag As Boolean
    Dim i As Long
    Dim strDirectory As String
    Dim Desired As String

    strDirectory = Application.ActiveWorkbook.Path & "\"
    i = 1
    flag = True
    varDirectory = Dir("C:\Users\folder\*", vbNormal)
    Range("A5:E100").clear

While flag = True
        If varDirectory = "" Then
            flag = False
        Else
        If varDirectory Like "*.mp4" Then
            Cells(i + 4, 1) = varDirectory
        End If
        If varDirectory Like "*.wav" Then
            Cells(i + 4, 2) = varDirectory
            i = i + 1
        End If
        If varDirectory Like "*.outreview" Then
            Cells(i + 4, 5) = varDirectory
        End If
        If varDirectory Like "*.out" Then
            Cells(i + 4, 4) = varDirectory
            Cells(i + 4, 3) = FileDateTime("C:\Users\folder\" & varDirectory)
        End If
        varDirectory = Dir
        End If
Wend
End Sub



Not sure about the modified date portion of the question, however, for the other, just include the extension in a row near the column header. 不确定问题的修改日期部分,但是对于其他问题,只需在列标题附近的行中包含扩展名即可。 Then use that to feed another loop. 然后使用它来填充另一个循环。

so for example: 因此,例如:

1) create a new row between rows 3 and 4. 2) in this new row 4, column A, store ".mp4". 1)在第3行和第4行之间创建一个新行。2)在此新行4的A列中存储“ .mp4”。 In B, ".wav", etc. 3) change your code to add another loop: (and use that loop # to reference the proper columns) 在B中,“。wav”等。3)更改代码以添加另一个循环:(并使用该循环#引用适当的列)

  Sub getfilelistfromfolder()
      Dim varDirectory As Variant
      Dim flag As Boolean
      Dim i As Long
      Dim strDirectory As String
      Dim Desired As String

  for x = 1 to 3

  Desired = Cells(4,x).Value

  strDirectory = Application.ActiveWorkbook.Path & "\"
  i = 1
  flag = True
  varDirectory = Dir("C:\Users\Folder\", vbNormal)

  While flag = True
      If varDirectory = "" Then
          flag = False
      Else
          If Right(varDirectory, 4) = Desired Then
              Cells(i + 4, x) = varDirectory
              i = i + 1
          End If
          varDirectory = Dir
      End If
  Wend
  next x

  End Sub

A different approach: 另一种方法:

Sub M_snb()
 c00 = "C:\Users\Folder\"
 sn = Split(CreateObject("wscript.shell").exec("cmd /c Dir """ & c00 & "*.*"" /b/a-d").stdout.readall, vbCrLf)

 ReDim sp(1 To UBound(sn), 5)

 With CreateObject("scripting.filesystemobject")
  For j = 0 To UBound(sn)
   c01 = lcase(.getextensionname(c00 & sn(j)))
   If c01 <> "" And InStr("mp4wavoutoutreview", c01) Then sp(j, Application.Match(c01, Array("mp4", "wav", "out", "", "outreview"), 0)-1) = sn(j)
   If c01 = "out" Then sp(j, 4) = FileDateTime(c00 & sn(j))
  Next
 End With

 sheet1.cells(1).resize(ubound(sp),ubound(sp,2))=sp
End Sub

This will do everything in one shot. 一次即可完成所有操作。 i haven't tested it. 我还没有测试。 Basically I use the Like operator and FileDateTime function. 基本上,我使用Like运算符和FileDateTime函数。 Let me know if that worked for you 让我知道这是否对您有用

Sub getfilelistfromfolder()
    Dim varDirectory As Variant
    Dim flag As Boolean
    Dim i As Long
    Dim strDirectory As String
    Dim Desired As String

    strDirectory = Application.ActiveWorkbook.Path & "\"
    i = 1
    flag = True
    varDirectory = Dir("C:\Users\Folder\*", vbNormal)
    Range("A5:D100").Clear

    While flag = True
        If varDirectory = "" Then
            flag = False
        ElseIf varDirectory Like "*.mp4" Then
            Cells(i + 4, 1) = varDirectory
            Cells(i + 4, 3) = FileDateTime("C:\Users\Folder\" & varDirectory)
            varDirectory = Dir
            i = i + 1
        ElseIf varDirectory Like "*.wav" Then
            Cells(i + 4, 2) = varDirectory
            Cells(i + 4, 3) = FileDateTime("C:\Users\Folder\" & varDirectory)
            varDirectory = Dir
            i = i + 1
        ElseIf varDirectory Like "*.outreview" Then
            Cells(i + 4, 5) = varDirectory
            Cells(i + 4, 3) = FileDateTime("C:\Users\Folder\" & varDirectory)
            varDirectory = Dir
            i = i + 1
        ElseIf varDirectory Like "*.out" Then
            Cells(i + 4, 4) = varDirectory
            Cells(i + 4, 3) = FileDateTime("C:\Users\Folder\" & varDirectory)
            varDirectory = Dir
            i = i + 1
        End If
    Wend
End Sub

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM