Solved

Replace multiple text values within every file of a selected folder

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

When you upgrade from Windows 8 to 8.1 or to Windows 10 or if you are like me you are on the Insider Program you may find yourself with many 450MB recovery partitions.  With a traditional disk that may not be a problem but with relatively smaller SS…
Use this article to create a batch file to backup a Microsoft SQL Server database to a Windows folder.  The folder can be on the local hard drive or on a network share.  This batch file will query the SQL server to get the current date & time and wi…
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…

919 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

14 Experts available now in Live!

Get 1:1 Help Now