Solved

Replace multiple text values within every file of a selected folder

Posted on 2014-09-11
4
1,203 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
4 Comments
 
LVL 85

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

Major Incident Management Communications

Major incidents and IT service outages cost companies millions. Often the solution to minimizing damage is automated communication. Find out more in our Major Incident Management Communications infographic.

Question has a verified solution.

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

Citrix XenApp, Internet Explorer 11 set to Enterprise Mode and using central hosted sites.xml file.
It’s been over a month into 2017, and there is already a sophisticated Gmail phishing email making it rounds. New techniques and tactics, have given hackers a way to authentically impersonate your contacts.How it Works The attack works by targeti…
This video Micro Tutorial explains how to clone a hard drive using a commercial software product for Windows systems called Casper from Future Systems Solutions (FSS). Cloning makes an exact, complete copy of one hard disk drive (HDD) onto another d…
In this video, we discuss why the need for additional vertical screen space has become more important in recent years, namely, due to the transition in the marketplace of 4x3 computer screens to 16x9 and 16x10 screens (so-called widescreen format). …

705 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