Photo Gallery Member List Search Calendars FAQ Ticket List Log Out


Read INI file, replace value once found with input from box.

 
Logged in as: Guest
arrSession:exec spGetSession 2,2,61786
 Active Users: There are 0 members and 0 guests.
 Users viewing this topic: none
 

 

 
  
  Printable Version
All Forums >> [Scripting] >> WSH & Client Side VBScript >> Read INI file, replace value once found with input from box.
  Do you like VisualBasicScript.com? Link to us and help spread the word about our forum. Thanks!
Page: [1]
Login
Message << Older Topic   Newer Topic >>
 Read INI file, replace value once found with input from... - 6/24/2008 3:20:51 AM   
  mp3droid

 

Posts: 11
Score: 0
Joined: 6/24/2008
Status: offline
First off let me dim that I am new to vbs scripting.

I have been reading posts of others that had similar issues. I have peiced together something close to what I want, but I've hit a wall.

I think my script thus far will:
1.) Sets strAnswer as user input from an Inputbox.
2.) Requires entry for the InputBox. (I'd like to require that it is a 3 digit number, but if complicated its not worth it).
3.) Sets objFile1 and objFile2 as file locations on the local machine.

What I need to do now is search for a line in those ini files that matches a value, and replace that entire line with some hard coded info, and the strAnswer.

Ex: search in objFile1 for the line that contains the string "StoreNum=", replace that entire line with "StoreNum=" & strAnswer
     search in objFile2 for the line that contains the string "StoreNum=", replace that entire line with "StoreNum=" & strAnswer

------------------------------------------------

Here's what I've got so far:

Const FOR_READING = 1
Const FOR_WRITING = 2
Const FOR_APPENDING = 8

Dim objFso
Dim objFile

Set objFso = CreateObject("Scripting.FileSystemObject")
Set objFile1 = objFso.OpenTextFile("f:\crs\store1\regdw.ini")
Set objFile2 = objFso.OpenTextFile("f:\crs\store1\regdw2.ini")

Do
   strAnswer = InputBox _
       ("Example: 123:","Enter the Store Number:")
   If strAnswer = "" Then
       Wscript.Echo "You MUST enter a store number!"
   Else
       ' here's where I don't know how to search and reaplce
       Exit Do
   End If
Loop

Set objFile = Nothing
Set objFso = Nothing

--------------------------------------------------

Thank, any help would be appreciated!
 
 
Post #: 1
 
 RE: Read INI file, replace value once found with input ... - 6/24/2008 4:56:29 AM   
  ebgreen


Posts: 4951
Score: 31
Joined: 7/12/2005
Status: online
So, I would suggest looking into Regular Expressions. If you search the forum you will find several examples. Basically you will use one Regex pattern to verify your 3 digit number requirement and you will use another pattern to do the replacement. Give the regex stuff a look and post back if you have problems.

_____________________________

"... when you are good and crazy, oooh, oooh, oooh, the sky is the limit!" - The Tick
Goog places to start:http://www.visualbasicscript.com/m_24727/tm.htm
http://www.visualbasicscript.com/m_47117/tm.htm

(in reply to mp3droid)
 
 
Post #: 2
 
 RE: Read INI file, replace value once found with input ... - 6/24/2008 5:41:44 AM   
  ehvbs

 

Posts: 2169
Score: 50
Joined: 6/22/2005
From: Germany
Status: offline
Hi mp3droid,

look here

http://www.visualbasicscript.com/fb.aspx?m=24833

Good luck!

ehvbs

(in reply to ebgreen)
 
 
Post #: 3
 
 RE: Read INI file, replace value once found with input ... - 6/24/2008 8:39:19 AM   
  mp3droid

 

Posts: 11
Score: 0
Joined: 6/24/2008
Status: offline
Ok guys, thanks for your help so far. Referencing the scripts I've found here and msdn on regular expressions I've come up with:


'To make Script Easier to Understand
Const FOR_READING = 1
Const FOR_WRITING = 2
Const FOR_APPENDING = 8

'Specifies Files to Read
Dim objFso
Dim objFile1
Dim objFile2

Set objFso = CreateObject("Scripting.FileSystemObject")
Set objFile1 = objFso.OpenTextFile("f:\crs\store1\regdw.ini",FOR_READING)
Set objFile2 = objFso.OpenTextFile("f:\crs\store1\regdw2.ini",FOR_READING)

'Creates RegEx & Pattern & Sets ignorecase to true
Dim sContent
Dim objStorenum
Dim oMTS

sContent = objFso.OpenTextFile(objFile1,ForReading).ReadAll
Set objStorenum = New RegExp
objStorenum.Pattern = "\r\n" + "storenum*" + "\r\n"
objStorenum.IgnoreCase = True


Now ehvbs, I read the post you linked to and I can't figure out what your script did after this point. I've still got to have it open objFile1 & objFile2 and replace objStorenum.pattern with the strAnswer from my first post.

I'm trying as hard as I can, but its hard to pick all this up at once. Help anyone?

(in reply to ehvbs)
 
 
Post #: 4
 
 RE: Read INI file, replace value once found with input ... - 6/24/2008 10:09:26 PM   
  ehvbs

 

Posts: 2169
Score: 50
Joined: 6/22/2005
From: Germany
Status: offline
Hi mp3droid,

I reworked the old script to handle replacement:


      

output:

      

Use he VBScript Docs (or google) to read about functions/methods like .CreateTextFile,
.Write, .Replace, and Replace. Feel free to ask for detailed explanations of lines you
don't understand.

Good luck!

ehvbs

(in reply to mp3droid)
 
 
Post #: 5
 
 RE: Read INI file, replace value once found with input ... - 6/25/2008 7:59:15 AM   
  mp3droid

 

Posts: 11
Score: 0
Joined: 6/24/2008
Status: offline


awesome! thanks for all your help, got it working now!

grazie!

(in reply to ehvbs)
 
 
Post #: 6
 
 RE: Read INI file, replace value once found with input ... - 6/26/2008 4:34:36 AM   
  Rischip


Posts: 502
Score: 2
Joined: 3/26/2007
Status: offline
Why reinvent the wheel? ....   http://www.fpschultze.de/smartfaq+faq.faqid+51.htm

_____________________________

Rischip
Author of - The Grim Linker

(in reply to mp3droid)
 
 
Post #: 7
 
 
 
  

If you found our site useful please link to us <a href="http://www.visualbasicscript.com">VisualBasicScript.com</a>.
All Forums >> [Scripting] >> WSH & Client Side VBScript >> Read INI file, replace value once found with input from box. Page: [1]
Jump to:





New Messages No New Messages
Hot Topic w/ New Messages Hot Topic w/o New Messages
Locked w/ New Messages Locked w/o New Messages
 Post New Thread
 Reply to Message
 Post New Poll
 Submit Vote
 Delete My Own Post
 Delete My Own Thread
 Rate Posts