Solved

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

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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

This tutorial is about how to put some of your C++ program's functionality into a standard DLL, and how to make working with the EXE and the DLL simple and seamless.   We'll be using Microsoft Visual Studio 2008 and we will cut out the noise; that i…
After several hours of googling I could not gather any information on this topic. There are several ways of controlling the USB port connected to any storage device. The best example of that is by changing the registry value of "HKEY_LOCAL_MACHINE\S…
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…
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…

829 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