Solved

Counting consecutive underscores and reducing to 1 in VBS (in a file name)

Posted on 2013-05-21
4
219 Views
Last Modified: 2013-05-22
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
0
Comment
Question by:tommy777
  • 2
  • 2
4 Comments
 
LVL 7

Accepted Solution

by:
BT15 earned 500 total points
ID: 39185728
would this work?

function renameFiles(nFolder)
   for each oFile in nFolder.files  
 if instr(oFile.name,"__") > 0 then
         newFileName = replace(oFile.name,"__","_")
         
         do until instr(newFileName ,"__") = 0
                newFileName = replace(newFileName,"__","_")
         loop
         
         newFilePath = left(oFile.path,len(oFile.path) - len(oFile.name)) & newFileName
         objFSO.MoveFile oFile.path, newFilePath
nCount = nCount+1    
end if
next
end function
0
 

Author Comment

by:tommy777
ID: 39186116
sorry, no that doesn't work...something wrong after the "do" statement
0
 

Author Comment

by:tommy777
ID: 39187854
actually it appears to work...thanks
0
 
LVL 7

Expert Comment

by:BT15
ID: 39187905
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.
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

This article shows how to make a Windows 7 gadget that accepts files dropped from the Windows Explorer.  It also illustrates how to give your gadget a non-rectangular shape and how to add some nifty visual effects to text displayed in a your gadget.…
zlib is a free compression library (a DLL) on which the popular gzip utility is built.  In this article, we'll see how to use the zlib functions to compress and decompress data in memory; that is, without needing to use a temporary file.  We'll be c…
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

746 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now