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.Fi leSystemOb ject")
nCount = 0
currentDirectory = left(WScript.ScriptFullNam e,(Len(WSc ript.Scrip tFullName) )-(len(WSc ript.Scrip tName)))
set rootFolder = objFSO.GetFolder(currentDi rectory)
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
Set objFSO = CreateObject("Scripting.Fi
nCount = 0
currentDirectory = left(WScript.ScriptFullNam
set rootFolder = objFSO.GetFolder(currentDi
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.
objFSO.MoveFile oFile.path, newFilePath
nCount = nCount+1
end if
next
end function
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
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.
Glad it worked for you.
ASKER