0

If have the following VBScript for recursively finding all the files in a set of folders. I simply found this on the web somewhere and can't take credit for it.

fileExtension = ".jpg"
folderPath = "C:\Pictures"
computerName = "."
arrFIL = Array()

If Right(folderPath,1) = "\" Then folderPath = Left(folderPath,Len(folderPath)-1)

Set wmiObject = GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & computerName & "\root\cimv2")
Set folderObject = wmiObject.Get("Win32_Directory='" & folderPath & "'")

EnumFolders folderObject, wmiObject, arrFIL
strFIL = UBound(arrFIL) + 1 & " files found with extension '" & fileExtension & "':" & vbCrLf & vbCrLf

For intFIL = 0 To UBound(arrFIL)
    Set objFile = objFSO.GetFile(arrFIL(intFIL))
    strFIL = strFIL & arrFIL(intFIL) & vbCrLf
Next
WScript.Echo strFIL

Sub EnumFolders(folderObject, wmiObject, arrFIL)
    On Error Resume Next
    Dim objSD1
    Dim objSD2
    Dim objFI1
    Dim objFI2
    Set objSD1 = wmiObject.ExecQuery("Associators of {Win32_Directory.Name='" & fold    erObject.Name & "'} Where AssocClass=Win32_SubDirectory ResultRole=PartComponent")

    For Each objSD2 in objSD1
        EnumFolders objSD2, wmiObject, arrFIL
    Next    
    On Error Goto 0

    Set objFI1 = wmiObject.ExecQuery("Associators of {Win32_Directory.Name='" & folderObject.Name & "'} Where ResultClass=CIM_DataFile")
    For Each objFI2 in objFI1
    If Right(objFI2.Name,Len(fileExtension)) = fileExtension Then
        intFIL = UBound(arrFIL) + 1
            ReDim Preserve arrFIL(intFIL)
            arrFIL(intFIL) = objFI2.Name
        End If
    Next
End Sub

What I need to do is run this against a bunch of folders, within C:\Pictures, and have it return all files where the Date Taken property of the photo is the 23rd of the month. Is this possible? How would I achieve this?

Thanks

Danny
  • 329
  • 3
  • 5
  • 12

1 Answers1

1

I'd use the Shell.Application object instead of WMI:

Const Name      =  0
Const DateTaken = 12

folderPath = "C:\Pictures"

Set re = New RegExp
re.Pattern = "[^0-9:./ ]"
re.Global  = True

Traverse CreateObject("Shell.Application").Namespace(folderPath)

Sub Traverse(fldr)
  For Each obj In fldr.Items
    If obj.IsFolder Then
      Traverse obj.GetFolder
    ElseIf LCase(obj.Type) = "jpeg image" Then
      If Day(re.Replace(fldr.GetDetailsOf(obj, DateTaken), "")) = 23 Then
        WScript.Echo fldr.GetDetailsOf(obj, Name)
      End If
    End If
  Next
End Sub
Ansgar Wiechers
  • 193,178
  • 25
  • 254
  • 328
  • Thanks for coming back to me so quickly. However, I receive `Type mismatch: '[string: "?23/?05/?2012 ??11:3"]'` as an error message. When I output the date taken for all the files in the folder, they appear to be wrapped in `?` – Danny Mar 29 '13 at 22:02
  • I also had to take out `ElseIf LCase(obj.Type) = "jpeg image" Then` and replace it with a standard `Else`. – Danny Mar 29 '13 at 22:05
  • 1
    I see. The `Date taken` field seems to use some kind of encoding. You can simply remove the additional characters with a regular expression, though (see updated answer). As for the type, you may need to adjust the reference string (`"jpeg image"`) to what the type is named on your system. If you want to process pictures of different types you have to extend the check (or remove it entirely if you want to process all files anyway). – Ansgar Wiechers Mar 30 '13 at 11:22
  • Excellent, thank you very much for your help. The amended script works exactly as I need it to. FYI, I needed to change `"jpeg image"` to `"jpg file"`. – Danny Apr 01 '13 at 12:22