WSH script to traverse files and folders in a folder structure and Replace Special Characters not allowed by Sharepoint, including multiple periods. Can be used to quickly go through a large number of files and folders, and prepare it for migration to a SharePoint document library.

Syntax: cscript.exe SPSCReplacer.vbs [>filename.log]

  • is the root folder to traverse
  • [>filename.log] pipes the output to a file.

where is requierd and [] is optional

[code title=”SPSCReplacer.vbs”]” Traverse files and folder in a folder structure and Replace Special Characters

” not allowed by SharePoint, including multiple periods

” Filename: SPSCReplacer.vbs (SharePoint Special Characters Replacer)

” Created by kmhuglen (at) gmail.com

” Version 0.2

Set fso = CreateObject(“Scripting.FileSystemObject”)

Set re = New RegExp

strPattern = “[~#%&~{}]” ‘Special characters not allowed in a Sharepoint filename’

strPattern2 = “.{2,}” ‘Multiple periods’

re.IgnoreCase = True

re.Global = True

If WScript.Arguments.Unnamed.Count = 1 Then

If fso.FolderExists(WScript.Arguments.Unnamed(0)) Then

Recurse fso.GetFolder(WScript.Arguments.Unnamed(0)), strPattern, “_”

Recurse fso.GetFolder(WScript.Arguments.Unnamed(0)), strPattern2, “.”

Else

WScript.Echo “Folder does not exist”

End If

Else

WScript.Echo “Syntax: cscript.exe SPSCReplacer.vbs [>SPSCReplacer.log]”

End If

Sub Recurse(f,p,v)

re.pattern = p

For Each sf In f.SubFolders

Recurse sf,p,v

If (sf.Name = re.Replace(sf.Name, v)) then

Else

WScript.Echo sf.path, ” -> “, re.Replace(sf.Name, v)

sf.Name = re.Replace(sf.Name, v)

End if

Next

For Each sf In f.Files

If (sf.Name = re.Replace(sf.Name, v)) then

Else

WScript.Echo sf.path, ” -> “, re.Replace(sf.Name, v)

sf.Name = re.Replace(sf.Name, v)

End if

Next[/code]