/build/static/layout/Breadcrumb_cap_w.png

Please help in writing a script to replace multiple words in a text file

I have several configuration files (over 40) and I need to replace multiple categories with the updated categories.  I have a csv file with one column containing the old categories and another column containing the new categories.

 

Ideally, I would like for the script to reference the csv file and be able to automatically run through all config files.

 

I have a very basic script that will replace one word with another word, but I have not been able to expand it beyond that.

 

This is the script I have;

 

 Const ForReading = 1
Const ForWriting = 2

Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.OpenTextFile("C:\Scripts\Text.txt", ForReading)

strText = objFile.ReadAll
objFile.Close
strNewText = Replace(strText, "Old", "New")
strNewText1 = Replace(strNewText, "Day", "Night")
strNewText2 = Replace(strNewText1, "1", "2")


Set objFile = objFSO.OpenTextFile("C:\Scripts\Text.txt", ForWriting)
objFile.WriteLine strNewText2

objFile.Close

2 Comments   [ + ] Show comments
  • You might want to consider using Perl.
    In the end you read in the categories and loop through each. You read in the file you want to modify just before the loop. You replace text inside the loop using the values you read in from the categories file. After the loop you save the file. - pcooper 10 years ago
    • yikes, i know even less about Perl than I do about vbs - d_frost 10 years ago
  • Your request is for something which - done properly - is a little complex. Consequently, you'll struggle to get anyone to spend any time building such a thing for you.

    As a pointer, I would suggest you read the CSV into an array and then loop through that array, passing the data into a function which does the replacement. - anonymous_9363 10 years ago

Answers (3)

Answer Summary:
Posted by: jaybee96 10 years ago
Red Belt
1

This is build-in functionality  with RayPack or  Installshield.


Comments:
  • Show us where in InstallShield there is native handling for reading a CSV! - anonymous_9363 10 years ago
    • http://helpnet.installshield.com/installshield16helplib/TextFileChanges-CreateRef.htm - jaybee96 10 years ago
    • it works like a charm! - jaybee96 10 years ago
    • can you let me know if this works for you?
      I can send you an example MSI ? - jaybee96 10 years ago
      • You've mis-read the brief (not that it's crystal clear, admittedly.)

        The OP wants to read the changes from a CSV and then apply them to OTHER FILES. - anonymous_9363 10 years ago
Posted by: terebent 10 years ago
Second Degree Brown Belt
1

I use this function to replase a string in a file: 

 Function ReplaceInFile(strFilePath, strToReplace, strNewValue)	Dim objFSO, objFile, strText, re	Set objFSO = CreateObject("Scripting.FileSystemObject")	if objFSO.FileExists(strFilePath) Then		Set objFile = objFSO.OpenTextFile(strFilePath, ForReading, True)			strText = objFile.ReadAll		objFile.Close		Set objFile = Nothing				strText = Replace(strText, strToReplace, strNewValue, 1, -1, 1)			Set objFile = objFSO.CreateTextFile(strFilePath, True)		objFile.Write strText		objFile.Close		Set objFile = Nothing		End If	Set objFSO = NothingEnd Function

 

If you need help to use it, please tell me.

 


Comments:
  • thank you! i will try this out, it requires less scripting than the one i was using - d_frost 10 years ago
Posted by: SMal.tmcc 10 years ago
Red Belt
0

you can replace entire strings of text also just by putting it in between quotes.  No idea on how to read a csv line by line to get the search and replace varibles

strNewText = Replace(strText, "Old string of text. to be replaced", "Is this the New text ?")

in this answer I replaced a string with another

http://www.itninja.com/question/number-of-autologins-required-has-changed-from-1-to-3-since-3-6-upgrade

strNewText = Replace(strText, "Old", "New")

Comments:
  • the reason i would like to use a csv file is because i have over 100 categories i need to change, so the script i have will work, i would just need to add more conditions for each category change, i was trying to avoid that, but i may have to end up doing that after all :/ - d_frost 10 years ago
 
This website uses cookies. By continuing to use this site and/or clicking the "Accept" button you are providing consent Quest Software and its affiliates do NOT sell the Personal Data you provide to us either when you register on our websites or when you do business with us. For more information about our Privacy Policy and our data protection efforts, please visit GDPR-HQ