Counting consecutive underscores and reducing to 1 in VBS (in a file name)
Posted on 2013-05-21
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)
wscript.echo nCount & " Files have been renamed."
for each sFolder in folder.subfolders
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