递归文件重命名

时间:2019-03-14 18:43:04

标签: vbscript

出于法律原因,我公司正在尝试从我们的整个文件系统中清除特定的缩写词。搜索将返回近30,000个上述首字母缩写词的实例。我使用建议here编写了以下VBS,以尝试使该过程递归。不幸的是,我未能正确实施它。

我在第3行中收到“无效的过程调用或参数”错误。 如果我编辑它以引用根文件夹,则会在第18行中得到一个必需的对象:“文件”错误。

Set objFso = CreateObject("Scripting.FileSystemObject")
Set Folder = objFSO.GetFolder("<folderpath>")

TraverseFolders objFso.GetFolder(strPath)

Function TraverseFolders(fldr)
    For Each File In Folder.SubFolders
        sNewFile = File.Name
        sNewFile = Replace(sNewFile, "old acronym", "new acronym")
        If (sNewFile <> File.Name) Then
            File.Move(File.ParentFolder + "\" + sNewFile)
        End If
    Next

    For Each sf In fldr.SubFolders
        TraverseFolders sf
    Next
    sNewFile = File.Name
    sNewFile = Replace(sNewFile, "old acronym", "new acronym")
    If (sNewFile <> File.Name) Then
        File.Move(File.ParentFolder + "\" + sNewFile)
    End If
End Function

要递归进行此工作,我缺少什么?

1 个答案:

答案 0 :(得分:-1)

对于那些迷路的人来说,问题在于定义了strPath并引用了Folder而不是fldr。 这是正确的代码:

Set objFso = CreateObject("Scripting.FileSystemObject")
Set strPath = objFSO.GetFolder("<folder path>")
'setting the strPath instead of the unused Folder'
TraverseFolders objFso.GetFolder(strPath)

Function TraverseFolders(fldr)

  For Each File In fldr.Files
  'Referencing fldr instead of the unused Folder'
    sNewFile = File.Name
    sNewFile = Replace(sNewFile,"old acronym","new acronym")
    if (sNewFile<>File.Name) then 
        File.Move(File.ParentFolder+"\"+sNewFile)
    end if

Next

  For Each sf In fldr.SubFolders
    TraverseFolders sf  
    'Removed unnecessary code duplicate'
    Next
End Function