Solved

Replace multiple text values within every file of a selected folder

Posted on 2014-09-11
4
1,004 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 83

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

0
 

Author Closing Comment

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

Featured Post

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

INTRODUCTION The purpose of this document is to demonstrate the Installation and configuration of the Data Protection Manager product. Note that this demonstration was prepared on the basis of Windows OS is 2008 R2 and DPM 2010. DATA PROTECTI…
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…
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…
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…

743 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

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now