Link to home
Start Free TrialLog in
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
ASKER CERTIFIED SOLUTION
Avatar of BT15
BT15
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of tommy777
tommy777

ASKER

sorry, no that doesn't work...something wrong after the "do" statement
actually it appears to work...thanks
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.