/build/static/layout/Breadcrumb_cap_w.png

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..

0 Comments   [ + ] Show comments

Answers (2)

Posted by: anonymous_9363 15 years ago
Red Belt
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.
Posted by: Charles Costa 15 years ago
Senior Yellow Belt
0
Thanks
Rating comments in this legacy AppDeploy message board thread won't reorder them,
so that the conversation will remain readable.
 
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