EDN Admin
Well-known member
Ive been working on an issue with a script I put together that will be used to replace a char or string en-mass. how it works:
1. Prompts user for search criteria<br/>
2. Prompts user for replacement criteria<br/>
3. Prompts user for target folder to run the script in, and then looks for all folders in that directory.
It then takes the information and recursively, one at a time looks in all of the subfolders found for an XML file, loads the contents of the file into a variable, and then does the search and replace, then saves the file and moves along to either the next
file, or next folder until complete.
It works great for any normal characters, however, the sole purpose of creating this is to replace unique characters like à and Ã. It seems to ignore this all together. I understand writing a regular expression will work around this, and
Ive found examples, but trying to implement those examples into my script have failed. Any advice would be appreciated. Im somewhat of a novice vb scripter, so dont bash the code too much
<div style="color:black; background-color:white
<pre><span style="color:green Folder search / find variables
<span style="color:blue Dim objFSO, objRootFolder, colSubfolders, strOutput
<span style="color:blue Dim objFolder, objShell
<span style="color:green String Search/Replace variables
<span style="color:blue Dim SearchMessage, ReplaceMessage, SearchVar, ReplaceVar
<span style="color:blue Dim Title, CancelMSG, SearchChar, strpath, strText, strNewText
<span style="color:green Target folder function variables
<span style="color:blue Dim oFSO, oFile, objFile, oShell
<span style="color:blue Set oFSO = CreateObject(<span style="color:#a31515 "Scripting.FileSystemObject")
<span style="color:blue Set oShell = CreateObject(<span style="color:#a31515 "Wscript.Shell")
<span style="color:green SearchMessage = Message prompted in the Search box
<span style="color:green ReplaceMessage = Message prompted in the Replacement box
<span style="color:green SearchVar = Variable saved from the Search box input
<span style="color:green ReplaceVar = Variable saved from the Replacement box input
<span style="color:green Title = Title of the dialogue box
<span style="color:green CancelMSG = Cancelation message
<span style="color:green SearchChar = "You Entered [txt]")
<span style="color:green objfolder = Folder selection var
<span style="color:green objShell = Folder selection var
<span style="color:green strPath = Folder selection path
<span style="color:green strText = File text loaded into the var
<span style="color:green strNewText = strText parsed and replaced with correct text
<span style="color:green Define dialog box variables.
SearchMessage = <span style="color:#a31515 "Search for a single character or string"
ReplaceMessage = <span style="color:#a31515 "Replacement character or string"
Title = <span style="color:#a31515 "Mass character / string replacer"
CancelMSG = <span style="color:#a31515 "User input canceled"
SearchChar = <span style="color:#a31515 "You entered:" & vbCrLf
<span style="color:green Input Box method.
<span style="color:green InputBox(prompt, title, default)
<span style="color:green prompt: The text shown in the dialog box
<span style="color:green title: The title of the dialog box
<span style="color:green default: Default value shown in the text box
<span style="color:green Dialogue box requesting search criteria
SearchVar = InputBox(SearchMessage, Title, <span style="color:#a31515 "Enter your search criteria")
<span style="color:green Evaluate the user search input.
<span style="color:blue If SearchMessage = <span style="color:#a31515 "" <span style="color:blue Then <span style="color:green Canceled by the user
WScript.Echo CancelMSG
<span style="color:blue Else
<span style="color:green WScript.Echo SearchChar & SearchVar Returns what you entered
<span style="color:blue End <span style="color:blue If
<span style="color:green Dialoguq box requesting replacement criteria
ReplaceVar = InputBox(ReplaceMessage, Title, <span style="color:#a31515 "Enter your replacement criteria")
<span style="color:green Evaluate the user replacement input.
<span style="color:blue If ReplaceMessage = <span style="color:#a31515 "" <span style="color:blue Then <span style="color:green Canceled by the user
WScript.Echo CancelMSG
<span style="color:blue Else
<span style="color:green WScript.Echo SearchChar & ReplaceVar Returns what you entered
<span style="color:blue End <span style="color:blue If
<span style="color:green Select the target folder
strPath = SelectFolder( <span style="color:#a31515 "" )
<span style="color:green Set object variables for file / folder search
<span style="color:blue Set objFSO = CreateObject(<span style="color:#a31515 "Scripting.FileSystemObject")
<span style="color:blue Set objRootFolder = objFSO.GetFolder(strPath)
<span style="color:blue Set colSubfolders = objRootFolder.SubFolders
<span style="color:green Complete the search and replace
<span style="color:blue If strPath = vbNull <span style="color:blue Then
WScript.Echo <span style="color:#a31515 "Cancelled"
<span style="color:blue Else <span style="color:blue For <span style="color:blue Each objFolder <span style="color:blue In colSubfolders
<span style="color:blue For <span style="color:blue Each oFile <span style="color:blue In objFolder.Files
<span style="color:blue If LCase(oFSO.GetExtensionName(oFile)) = <span style="color:#a31515 "xml" <span style="color:blue Then
<span style="color:blue If oFile.size > 0 <span style="color:blue Then
<span style="color:blue Set objFile = oFSO.OpenTextFile(oFile, 1,, 0)
strText = objFile.ReadAll
objFile.Close
strNewText = Replace(strText, SearchVar, ReplaceVar)
<span style="color:blue Set objFile = oFSO.OpenTextFile(oFile, 2,, 0)
objFile.WriteLine strNewText
objFile.Close
<span style="color:blue End <span style="color:blue If
<span style="color:blue End <span style="color:blue If
<span style="color:blue Next
<span style="color:blue Next
oShell.Popup <span style="color:#a31515 "Process completed",,, 0 + 64
<span style="color:blue End <span style="color:blue If
<span style="color:green Function that handles selecting the target folder
<span style="color:blue Function SelectFolder( myStartFolder )
<span style="color:green error handling
<span style="color:blue On <span style="color:blue Error <span style="color:blue Resume <span style="color:blue Next
SelectFolder = vbNull
<span style="color:green create dialogue box
<span style="color:blue Set objShell = CreateObject( <span style="color:#a31515 "Shell.Application" )
<span style="color:blue Set objFolder = objShell.BrowseForFolder( 0,<span style="color:#a31515 "Select a Folder. This will search for xml files in the subfolders.", 0, myStartFolder )
<span style="color:green Return the path
<span style="color:blue If IsObject( objfolder ) <span style="color:blue Then SelectFolder = objFolder.Self.Path
<span style="color:green cleanup
<span style="color:blue Set objFolder = <span style="color:blue Nothing
<span style="color:blue Set objshell = <span style="color:blue Nothing
<span style="color:blue On <span style="color:blue Error <span style="color:blue Goto 0
<span style="color:blue End <span style="color:blue Function
<span style="color:green *** End
[/code]
<br/>
<br/>
<br/>
View the full article
1. Prompts user for search criteria<br/>
2. Prompts user for replacement criteria<br/>
3. Prompts user for target folder to run the script in, and then looks for all folders in that directory.
It then takes the information and recursively, one at a time looks in all of the subfolders found for an XML file, loads the contents of the file into a variable, and then does the search and replace, then saves the file and moves along to either the next
file, or next folder until complete.
It works great for any normal characters, however, the sole purpose of creating this is to replace unique characters like à and Ã. It seems to ignore this all together. I understand writing a regular expression will work around this, and
Ive found examples, but trying to implement those examples into my script have failed. Any advice would be appreciated. Im somewhat of a novice vb scripter, so dont bash the code too much
<div style="color:black; background-color:white
<pre><span style="color:green Folder search / find variables
<span style="color:blue Dim objFSO, objRootFolder, colSubfolders, strOutput
<span style="color:blue Dim objFolder, objShell
<span style="color:green String Search/Replace variables
<span style="color:blue Dim SearchMessage, ReplaceMessage, SearchVar, ReplaceVar
<span style="color:blue Dim Title, CancelMSG, SearchChar, strpath, strText, strNewText
<span style="color:green Target folder function variables
<span style="color:blue Dim oFSO, oFile, objFile, oShell
<span style="color:blue Set oFSO = CreateObject(<span style="color:#a31515 "Scripting.FileSystemObject")
<span style="color:blue Set oShell = CreateObject(<span style="color:#a31515 "Wscript.Shell")
<span style="color:green SearchMessage = Message prompted in the Search box
<span style="color:green ReplaceMessage = Message prompted in the Replacement box
<span style="color:green SearchVar = Variable saved from the Search box input
<span style="color:green ReplaceVar = Variable saved from the Replacement box input
<span style="color:green Title = Title of the dialogue box
<span style="color:green CancelMSG = Cancelation message
<span style="color:green SearchChar = "You Entered [txt]")
<span style="color:green objfolder = Folder selection var
<span style="color:green objShell = Folder selection var
<span style="color:green strPath = Folder selection path
<span style="color:green strText = File text loaded into the var
<span style="color:green strNewText = strText parsed and replaced with correct text
<span style="color:green Define dialog box variables.
SearchMessage = <span style="color:#a31515 "Search for a single character or string"
ReplaceMessage = <span style="color:#a31515 "Replacement character or string"
Title = <span style="color:#a31515 "Mass character / string replacer"
CancelMSG = <span style="color:#a31515 "User input canceled"
SearchChar = <span style="color:#a31515 "You entered:" & vbCrLf
<span style="color:green Input Box method.
<span style="color:green InputBox(prompt, title, default)
<span style="color:green prompt: The text shown in the dialog box
<span style="color:green title: The title of the dialog box
<span style="color:green default: Default value shown in the text box
<span style="color:green Dialogue box requesting search criteria
SearchVar = InputBox(SearchMessage, Title, <span style="color:#a31515 "Enter your search criteria")
<span style="color:green Evaluate the user search input.
<span style="color:blue If SearchMessage = <span style="color:#a31515 "" <span style="color:blue Then <span style="color:green Canceled by the user
WScript.Echo CancelMSG
<span style="color:blue Else
<span style="color:green WScript.Echo SearchChar & SearchVar Returns what you entered
<span style="color:blue End <span style="color:blue If
<span style="color:green Dialoguq box requesting replacement criteria
ReplaceVar = InputBox(ReplaceMessage, Title, <span style="color:#a31515 "Enter your replacement criteria")
<span style="color:green Evaluate the user replacement input.
<span style="color:blue If ReplaceMessage = <span style="color:#a31515 "" <span style="color:blue Then <span style="color:green Canceled by the user
WScript.Echo CancelMSG
<span style="color:blue Else
<span style="color:green WScript.Echo SearchChar & ReplaceVar Returns what you entered
<span style="color:blue End <span style="color:blue If
<span style="color:green Select the target folder
strPath = SelectFolder( <span style="color:#a31515 "" )
<span style="color:green Set object variables for file / folder search
<span style="color:blue Set objFSO = CreateObject(<span style="color:#a31515 "Scripting.FileSystemObject")
<span style="color:blue Set objRootFolder = objFSO.GetFolder(strPath)
<span style="color:blue Set colSubfolders = objRootFolder.SubFolders
<span style="color:green Complete the search and replace
<span style="color:blue If strPath = vbNull <span style="color:blue Then
WScript.Echo <span style="color:#a31515 "Cancelled"
<span style="color:blue Else <span style="color:blue For <span style="color:blue Each objFolder <span style="color:blue In colSubfolders
<span style="color:blue For <span style="color:blue Each oFile <span style="color:blue In objFolder.Files
<span style="color:blue If LCase(oFSO.GetExtensionName(oFile)) = <span style="color:#a31515 "xml" <span style="color:blue Then
<span style="color:blue If oFile.size > 0 <span style="color:blue Then
<span style="color:blue Set objFile = oFSO.OpenTextFile(oFile, 1,, 0)
strText = objFile.ReadAll
objFile.Close
strNewText = Replace(strText, SearchVar, ReplaceVar)
<span style="color:blue Set objFile = oFSO.OpenTextFile(oFile, 2,, 0)
objFile.WriteLine strNewText
objFile.Close
<span style="color:blue End <span style="color:blue If
<span style="color:blue End <span style="color:blue If
<span style="color:blue Next
<span style="color:blue Next
oShell.Popup <span style="color:#a31515 "Process completed",,, 0 + 64
<span style="color:blue End <span style="color:blue If
<span style="color:green Function that handles selecting the target folder
<span style="color:blue Function SelectFolder( myStartFolder )
<span style="color:green error handling
<span style="color:blue On <span style="color:blue Error <span style="color:blue Resume <span style="color:blue Next
SelectFolder = vbNull
<span style="color:green create dialogue box
<span style="color:blue Set objShell = CreateObject( <span style="color:#a31515 "Shell.Application" )
<span style="color:blue Set objFolder = objShell.BrowseForFolder( 0,<span style="color:#a31515 "Select a Folder. This will search for xml files in the subfolders.", 0, myStartFolder )
<span style="color:green Return the path
<span style="color:blue If IsObject( objfolder ) <span style="color:blue Then SelectFolder = objFolder.Self.Path
<span style="color:green cleanup
<span style="color:blue Set objFolder = <span style="color:blue Nothing
<span style="color:blue Set objshell = <span style="color:blue Nothing
<span style="color:blue On <span style="color:blue Error <span style="color:blue Goto 0
<span style="color:blue End <span style="color:blue Function
<span style="color:green *** End
[/code]
<br/>
<br/>
<br/>
View the full article