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..
0 Comments   [ + ] Show Comments

Comments

Please log in to comment

Rating comments in this legacy AppDeploy message board thread won't reorder them,
so that the conversation will remain readable.

Answers

0
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!):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.
Answered 12/23/2008 by: VBScab
Red Belt

Please log in to comment
0
Thanks
Answered 01/05/2009 by: Charles Costa
Senior Yellow Belt

Please log in to comment
Answer this question or Comment on this question for clarity