Problem with a VB Script
Hello...
I need that this script open a INI file read it and replace some entries, but the script only replace the first entry...
This is the Script:
On Error Resume Next
Const ForReading = 1
Const ForWriting = 2
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.OpenTextFile("C:\APL\FOS10\PRD\pic.ini", ForReading)
strText = objFile.ReadAll
objFile.Close
strNewText = Replace(strText, "C:\APL\FOS\PIC", "C:\APL\FOS10\PRD\PIC")
strNewText = Replace(strText, "C:\APL\fos\PIC", "C:\APL\FOS10\PRD\PIC")
strNewText = Replace(strText, "C:\APL\FOS\pic", "C:\APL\FOS10\PRD\PIC")
strNewText = Replace(strText, "C:\APL\fos\pic", "C:\APL\FOS10\PRD\PIC")
strNewText = Replace(strText, "C:\apl\FOS\PIC", "C:\APL\FOS10\PRD\PIC")
strNewText = Replace(strText, "C:\apl\fos\PIC", "C:\APL\FOS10\PRD\PIC")
strNewText = Replace(strText, "C:\apl\FOS\pic", "C:\APL\FOS10\PRD\PIC")
strNewText = Replace(strText, "C:\apl\fos\pic", "C:\APL\FOS10\PRD\PIC")
Set objFile = objFSO.OpenTextFile("C:\APL\FOS10\PRD\pic.ini", ForWriting)
objFile.WriteLine strNewText
objFile.Close
Thanks..
I need that this script open a INI file read it and replace some entries, but the script only replace the first entry...
This is the Script:
On Error Resume Next
Const ForReading = 1
Const ForWriting = 2
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.OpenTextFile("C:\APL\FOS10\PRD\pic.ini", ForReading)
strText = objFile.ReadAll
objFile.Close
strNewText = Replace(strText, "C:\APL\FOS\PIC", "C:\APL\FOS10\PRD\PIC")
strNewText = Replace(strText, "C:\APL\fos\PIC", "C:\APL\FOS10\PRD\PIC")
strNewText = Replace(strText, "C:\APL\FOS\pic", "C:\APL\FOS10\PRD\PIC")
strNewText = Replace(strText, "C:\APL\fos\pic", "C:\APL\FOS10\PRD\PIC")
strNewText = Replace(strText, "C:\apl\FOS\PIC", "C:\APL\FOS10\PRD\PIC")
strNewText = Replace(strText, "C:\apl\fos\PIC", "C:\APL\FOS10\PRD\PIC")
strNewText = Replace(strText, "C:\apl\FOS\pic", "C:\APL\FOS10\PRD\PIC")
strNewText = Replace(strText, "C:\apl\fos\pic", "C:\APL\FOS10\PRD\PIC")
Set objFile = objFSO.OpenTextFile("C:\APL\FOS10\PRD\pic.ini", ForWriting)
objFile.WriteLine strNewText
objFile.Close
Thanks..
0 Comments
[ + ] Show comments
Answers (2)
Please log in to answer
Posted by:
anonymous_9363
15 years ago
Find a class file clsINI.VBS on the web and use that. It will enable you to specify the sections which contain the text you want to replace. IIRC, it's on http://www.jsware.net as part of a 'pack', but don't quote me.
If you choose to persist with your script, you need to abandon the ReadAll method and loop through the file one line at a time (ReadLine), check for the presence of the string to be replaced and, if it's present, replace it. Again, however, there must be thousands of scripts for download which loop through a text file and replace text. Why re-invent the wheel? :)
Next, use variables to avoid needless repetition through your scripts (and make subsequent edits to it MUCH quicker!):
If you choose to persist with your script, you need to abandon the ReadAll method and loop through the file one line at a time (ReadLine), check for the presence of the string to be replaced and, if it's present, replace it. Again, however, there must be thousands of scripts for download which loop through a text file and replace text. Why re-invent the wheel? :)
Next, use variables to avoid needless repetition through your scripts (and make subsequent edits to it MUCH quicker!):
strTextToSearchFor = "C:\APL\FOS\PIC"
strTextToReplaceWith = "C:\APL\FOS10\PRD\PIC"
strNewText = Replace(strText, strTextToSearchFor, strTextToReplaceWith)
Lastly, ALWAYS use 'Option Explicit' at the top of your scripts. That will ensure you get into the habit of properly declaring variables.
Rating comments in this legacy AppDeploy message board thread won't reorder them,
so that the conversation will remain readable.
so that the conversation will remain readable.