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
Answers (3)
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
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
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
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
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
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