Solved

Replace multiple text values within every file of a selected folder

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

1
 

Author Closing Comment

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

Featured Post

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

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

Not long ago I saw a question in the VB Script forum that I thought would not take much time. You can read that question (Question ID  (http://www.experts-exchange.com/Programming/Languages/Visual_Basic/VB_Script/Q_28455246.html)28455246) Here (http…
You may have already been in the need to update a whole folder stucture using a script. Robocopy does it well and even provides a list of non-updated files in a log (if asked to). Generally those files that were locked by a user or a process by the …
Windows 8 comes with a dramatically different user interface known as Metro. Notably missing from the new interface is a Start button and Start Menu. Many users do not like it, much preferring the interface of earlier versions — Windows 7, Windows X…
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…

772 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