Avatar of tommy777
tommy777
 asked on

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
Microsoft Development

Avatar of undefined
Last Comment
BT15

8/22/2022 - Mon
ASKER CERTIFIED SOLUTION
BT15

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
tommy777

ASKER
sorry, no that doesn't work...something wrong after the "do" statement
tommy777

ASKER
actually it appears to work...thanks
BT15

phew. i was going to spend some time pouring over that code again. It has been a while since i have used VBS.

Glad it worked for you.
Experts Exchange has (a) saved my job multiple times, (b) saved me hours, days, and even weeks of work, and often (c) makes me look like a superhero! This place is MAGIC!
Walt Forbes