Solved

Replace multiple text values within every file of a selected folder

Posted on 2014-09-11
4
1,101 Views
Last Modified: 2014-09-16
I have searched and tried all kinds of scripts and have not found what I am looking for. I cannot use a shareware app as it is against the network security rules to install any shareware. I have to use standard scripting tools.

I have a large number of text files that need to have multiple values changed in each file. I have found a script that will make the change but I have to set the file name, string to search, and string to replace each time. I want to be able to create a list of values to search for and their corresponding replacement value. Then set a base folder that the code will search each file within the folder and replace each of the listed values. i.e. Within every file in a designated folder replace every instance of "Thomas" with "Tom", every instance of "Susan" with "Sue" etc. The code I am using now is:

Option Explicit

Dim fso,strFilename,strSearch,strReplace,objFile,oldContent,newContent
 
strFilename="C:\Files\Test1.txt"
strSearch="Thomas"
strReplace="Tom"
 
'Does file exist?
Set fso=CreateObject("Scripting.FileSystemObject")
if fso.FileExists(strFilename)=false then
   wscript.echo "file not found!"
   wscript.Quit
end if
 
'Read file
set objFile=fso.OpenTextFile(strFilename,1)
oldContent=objFile.ReadAll
 
'Write file
newContent=replace(oldContent,strSearch,strReplace,1,-1,0)
set objFile=fso.OpenTextFile(strFilename,2)
objFile.Write newContent
objFile.Close

Open in new window

0
Comment
Question by:SCMCEN
  • 2
4 Comments
 
LVL 84

Expert Comment

by:oBdA
ID: 40318048
How about a few lines of Powershell? This will overwrite the processed files (as your script does) but could easily be changed to use new names or a different target directory. The replacement is currently case sensitive.
$BaseFolder = "C:\Files"
$Filter = "*.txt"
$Replace = @{
	"Thomas" = "Tom"
	"Susan" = "Sue"
}
Get-ChildItem -Path $BaseFolder -Filter $Filter | % {
	"Processing $($_.FullName) ..." | Write-Host
	$Content = Get-Content -Path $_.FullName | Out-String
	$Replace.GetEnumerator() | % {$Content = $Content.Replace($_.Name, $_.Value)}
	$Content | Set-Content -Path $_.FullName
}

Open in new window

0
 

Author Comment

by:SCMCEN
ID: 40318467
Thanks for the quick response. Unfortunately Powershell cannot be run on my machine due to network security rules.
0
 
LVL 35

Accepted Solution

by:
Robert Schutt earned 500 total points
ID: 40320907
Maybe something like this:
Option Explicit

Dim fso, strFolder, strFilename, strSearch, strReplace, objFile, oldContent, newContent, intCounter

strFolder = "C:\Files"
strSearch = Array("Thomas","Susan")
strReplace = Array("Tom","Sue")

If UBound(strSearch) <> UBound(strReplace) Then
   Wscript.Echo "search/replace arrays are not the same length!"
   Wscript.Quit
End If

Set fso = CreateObject("Scripting.FileSystemObject")

For Each strFilename In fso.GetFolder(strFolder).Files
  If Right(strFilename, 4) = ".txt" Then
    'Read file
    Set objFile = fso.OpenTextFile(strFilename, 1)
    oldContent = objFile.ReadAll
    newContent = oldContent

    'Do all the replaces
    For intCounter = 0 To UBound(strSearch)
      newContent = Replace(newContent, strSearch(intCounter), strReplace(intCounter), 1, -1, 0)
    Next

    'Write file
    If newContent <> oldContent Then
      set objFile = fso.OpenTextFile(strFilename, 2)
      objFile.Write newContent
      objFile.Close
    End If
  End If
Next

Open in new window

1
 

Author Closing Comment

by:SCMCEN
ID: 40326322
Perfect and simple answer. Thank You!!
0

Featured Post

MIM Survival Guide for Service Desk Managers

Major incidents can send mastered service desk processes into disorder. Systems and tools produce the data needed to resolve these incidents, but your challenge is getting that information to the right people fast. Check out the Survival Guide and begin bringing order to chaos.

Question has a verified solution.

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

Suggested Solutions

This article was inspired by a question here at Experts Exchange (http://www.experts-exchange.com/Software/Photos_Graphics/Images_and_Photos/Q_28629170.html). The requirements stated in that question are (1) reduce the file size of a large number of…
When you see single cell contains number and text, and you have to get any date out of it seems like cracking our heads.
Windows 8 came with a dramatically different user interface known as Metro. Notably missing from that interface was a Start button and Start Menu. Microsoft responded to negative user feedback of the Metro interface, bringing back the Start button a…
With the advent of Windows 10, Microsoft is pushing a Get Windows 10 icon into the notification area (system tray) of qualifying computers. There are many reasons for wanting to remove this icon. This two-part Experts Exchange video Micro Tutorial s…

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