尝试在 VBScript 中使用 Shell 对象和 FileSystemObject 进行文件操作 [英] Trying to use Shell object and FileSystemObject in VBScript for file manipulation

查看:37
本文介绍了尝试在 VBScript 中使用 Shell 对象和 FileSystemObject 进行文件操作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图递归地遍历数百个目录和数千个 JPG 文件,以按日期对新文件夹中的文件进行排序.到目前为止,我能够使用 Shell NameSpace 对象单独 GetDetailsOf 文件,并且我还能够使用 FileSystemObject 递归地循环遍历目录.但是,当我尝试将它们放在函数等中时,当我尝试从照片中获取 DateTaken 属性时,却一无所获.

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.

这是我目前的代码:

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

推荐答案

访问 arrDate(0) 时出现下标超出范围"错误是由 arrDate 为空 (UBound(arrDate) == -1) 引起的.由于对非空字符串进行拆分将返回一个数组,即使未找到分隔符,并且尝试拆分 Null 将引发无效使用 Null"错误,因此我们可以确定 strFileNameDate 为".

The "Subscript out of range" error when accessing arrDate(0) is caused by arrDate being empty (UBound(arrDate) == -1). As a Split on a non-empty string will return an array, even if the separator is not found, and an attempt to Split Null will raise an "Invalid use of Null" error, we can be sure that strFileNameDate is "".

可能的原因:

  1. 拍摄日期"的索引是 25 (XP) 而不是 12 (Win 7) - 或者任何盖茨先生想到的 Win 8.
  2. 未填写 DPT 属性.
  3. 你的清洁功能搞砸了.

您必须测试包含有效日期的 strFileNameDate 并决定将没有有效 DPT 的文件放在哪里.

You have to test for strFileNameDate containing a valid date and decide where to put the files without a valid DPT.

附言与其进行递归循环,不如考虑使用

P.S. Instead of doing the recursive loopings, you should consider to use

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

并处理该文件.

这篇关于尝试在 VBScript 中使用 Shell 对象和 FileSystemObject 进行文件操作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆