1

I am trying to recursively loop through hundreds of directories, and thousands of JPG files to gather sort the files in new folders by date. So far, I am able to individually GetDetailsOf the files using the Shell NameSpace object, and I am also able to recursively loop through directories using the FileSystemObject. However, when I try to put them together in functions, etc, I am getting nothing back when I try to get the DateTaken attribute from the photo.

Here is my code so far:

sFolderPathspec = "C:\LocationOfFiles"

Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objDir = objFSO.GetFolder(sFolderPathspec)

Dim arrFiles()


getInfo(objDir)

Sub getInfo(pCurrentDir)
    fileCount = 0
    For Each strFileName In pCurrentDir.Files
        fileCount = fileCount + 1
    Next

    ReDim arrFiles(fileCount,2) 

    i=0
    For Each aItem In pCurrentDir.Files

        wscript.Echo aItem.Name
        arrFiles(i,0) = aItem.Name
        strFileName = aItem.Name
        strDir = pCurrentDir.Path
        wscript.echo strDir
        dateVar = GetDatePictureTaken(strFileName, strDir)  
        'dateVar = Temp2 & "_" & Temp3 & "_" & Temp1
        arrFiles(i,1) = dateVar
        WScript.echo i & "." & "M:" & monthVar & " Y:" & yearVar
        WScript.echo i & "." & strFileName & " : " & arrFiles(i,1) & " : " & dateVar
        i=i+1
    Next

    For Each aItem In pCurrentDir.SubFolders
       'wscript.Echo aItem.Name & " passing recursively"
       getInfo(aItem)
    Next

End Sub

Function GetDatePictureTaken(strFileName, strDir)

    Set objShell = CreateObject ("Shell.Application")
    Set objCurrFolder = objShell.Namespace(strDir)

    'wscript.Echo cstr(objCurrFolder.GetDetailsOf(strFileName, 12))

    strFileNameDate = cstr(objCurrFolder.GetDetailsOf(strFileName, 12))
    strFileNameDate = CleanNonDisplayableCharacters(strFileNameDate)
    arrDate = split(strFileNameDate, "/")
    '''FAILS HERE WITH A SUBSCRIPT OUT OF RANGE ERROR SINCE IT GETS NULL VALUES BACK FROM THE GET DETAILS OF FUNCTION'''
    monthVar = arrDate(0)
    yearVar = arrDate(1)
    dayVar = arrDate(2)

    GetDatePictureTaken = monthVar & "\" & dayVar & "\" & yearVar

End Function


Function CleanNonDisplayableCharacters(strInput)

      strTemp = ""
      For i = 1 to len(strInput)
          strChar = Mid(strInput,i,1)
          If Asc(strChar) < 126 and not Asc(strChar) = 63 Then
              strTemp = strTemp & strChar
          End If
      Next
      CleanNonDisplayableCharacters = strTemp

End Function
4

1 に答える 1

0

arrDate(0)にアクセスするときの「添え字が範囲外です」エラーは、arrDateが空(UBound(arrDate)== -1)であることが原因で発生します。空でない文字列でSplitを実行すると、区切り文字が見つからない場合でも配列が返され、Nullを分割しようとすると「無効なNullの使用」エラーが発生するため、strFileNameDateは「」であると確信できます。

その考えられる理由:

  1. 「撮影日」のインデックスは25(XP)であり、12(Win 7)ではありません。または、Win8でゲイツ氏の頭に浮かんだものは何でもです。
  2. DPTプロパティは入力されていません。
  3. あなたの掃除機能はそれを台無しにしました。

有効な日付を含むstrFileNameDateをテストし、有効なDPTなしでファイルを配置する場所を決定する必要があります。

PS再帰的なループを実行する代わりに、使用を検討する必要があります

dir /s/b path\*.jpg > pictures.txt

そしてそのファイルを処理します。

于 2013-01-10T09:05:37.830 に答える