Solved

Vbscript to go through files

Posted on 2011-09-23
4
318 Views
Last Modified: 2012-06-21
Need a vb script that will go through files names in a directory and find "&" and replace it with "and"
0
Comment
Question by:chasmant
  • 2
  • 2
4 Comments
 
LVL 6

Expert Comment

by:effes
ID: 36589166
Have a look at the script on this page:
http://coderstalk.blogspot.com/2007/09/vbscript-to-replace-underscores-in.html

You will just have to tweak it a bit to do what you want. Like this:
' check if the file name contains underscore
If InStr(1, fil.Name, "&") <> 0 Then    '<-- replaced with ampersand
    ' replace underscore with space
    sName = Replace(fil.Name, "&", "and")    '<-- replaced with ampersand and "and"

Open in new window

Hope that helps.
0
 
LVL 1

Author Comment

by:chasmant
ID: 36589187
sorry I meant folder names not file names
so I would give it a path and then have it look through all sub directories and find "&" and remplace it with "and"
0
 
LVL 6

Accepted Solution

by:
effes earned 500 total points
ID: 36589386
OK. I modified the script from the above link to rename all the folders and subfolders, starting in the folder where the script is located. I marked the line in the code where you can change the starting folder to whatever you like.
Dim sStartFolder
sStartFolder = "."    ' <-- replace this with the path to the folder you want to start in

RenameSubFolders(sStartFolder)

Sub RenameSubFolders(sFolder)
	Dim fso
	Dim fol

	' create the filesystem object
	Set fso = WScript.CreateObject("Scripting.FileSystemObject")
		
	' go thru each files in the folder
	For Each folder In fso.GetFolder(sFolder).SubFolders
		' check if the file name contains underscore
		If InStr(1, folder.Name, "_") <> 0 Then
			' replace underscore with space
			sName = Replace(folder.Name, "_", "M")
			' rename the foldere
			folder.Name = sName
		End If
		
		RenameSubFolders(folder.Path)
	Next
End Sub


' echo the job is completed
WScript.Echo "Completed!"

Open in new window

0
 
LVL 1

Author Closing Comment

by:chasmant
ID: 36590442
thanksd will really save time
0

Featured Post

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone 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

I met Paul Devereux (@pdevereux) today when I responded to his tweet asking “Anybody know how to automate adding files from disk to a folder in #outlook  ?”.  I replied back and told Paul that using automation, in this case scripting, to add files t…
Over the years I have built up my own little library of code snippets that I refer to when programming or writing a script.  Many of these have come from the web or adaptations from snippets I find on the Web.  Periodically I add to them when I come…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…

831 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