0

For legal reasons, my company is trying to scrub a specific acronym from our entire file system. A search returns nearly 30,000 instances of said acronym. I have written the following VBS using the suggestions here to attempt to make the process recursive. Unfortunately, I failed to properly implement it.

I am getting an "Invalid procedure call or argument" error in line 3. If I edit it to reference the root folder, I instead get an Object required: 'File' error in line 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

What am I missing to make this work recursively?

Ansgar Wiechers
  • 193,178
  • 25
  • 254
  • 328

1 Answers1

-1

For those who stumble here, the issue was defining the strPath and referencing Folder instead of fldr. Here is the correct code:

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