Solved

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

Posted on 2013-05-21
4
221 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

If you have ever found yourself doing a repetitive action with the mouse and keyboard, and if you have even a little programming experience, there is a good chance that you can use a text editor to whip together a sort of macro to automate the proce…
In this article, I will show how to use the Ribbon IDs Tool Window to assign the built-in Office icons to a ribbon button.  This tool will help us to find the OfficeImageId that corresponds to our desired built-in Office icon. The tool is part of…
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…
This is a video that shows how the OnPage alerts system integrates into ConnectWise, how a trigger is set, how a page is sent via the trigger, and how the SENT, DELIVERED, READ & REPLIED receipts get entered into the internal tab of the ConnectWise …

932 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

13 Experts available now in Live!

Get 1:1 Help Now