Counting consecutive underscores and reducing to 1 in VBS (in a file name)
In the code below, I have two underscores being replace by one underscore. I would like to have any number of consecutive underscores replaced by one underscore. So if there are 6 consecutive underscores in a file name, running the script will reduce that to one.
Set objFSO = CreateObject("Scripting.FileSystemObject")
nCount = 0
currentDirectory = left(WScript.ScriptFullName,(Len(WScript.ScriptFullName))-(len(WScript.ScriptName)))
set rootFolder = objFSO.GetFolder(currentDirectory)
renameFiles rootfolder
enumFolders rootFolder
wscript.echo nCount & " Files have been renamed."
function enumFolders(folder)
for each sFolder in folder.subfolders
renameFiles sFolder
enumFolders sFolder
next
end function
function renameFiles(nFolder)
for each oFile in nFolder.files
if instr(oFile.name,"__") > 0 then
newFileName = replace(oFile.name,"__","_")
newFilePath = left(oFile.path,len(oFile.path) - len(oFile.name)) & newFileName
objFSO.MoveFile oFile.path, newFilePath
nCount = nCount+1
end if
next
end function