Solved

vbs - multiple edits on a ini file

Posted on 2008-10-12
9
287 Views
Last Modified: 2012-06-27
I have a ini file I need to replace 3 lines with new values, (some of the lines can be found more than one time)

The code I have below works for replacing these lines, but the only way I can get it to work is to open the file and edit the file 3 seperate times which not such a good way to do it.

What I want to do is just open the file one time and edit the 3 values whereever they are found then close the file so I don't have to do all the opening, editing and closeing 3 seperate times.

The 3 lines I need to replace start with:
popserver=
popaccount=
checkformailevery=

Example, in this file I have these 3 lines

popserver=mail.abc.com
popaccount=user@mail.xyz.com
checkformailevery=20

So want to make them look like:

PopServer=NO_Check_Mail
POPAccount=NO_Mail-pop
CheckForMailEvery=0


popserver=mail.abc.com replace with PopServer=NO_Check_Mail
popaccount=user@mail.xyz.com replace with POPAccount=NO_Mail-pop
checkformailevery=20 replace with CheckForMailEvery=0


The following 3 peices of code will do what I want if I go through the process of opening and editing the file 3 seperate times. How can I open the file once and make the edits I need.


Const Reading = 1
Const Writing = 2
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objTextFile = objFSO.OpenTextFile("c:\temp\test.ini", Reading)
Do Until objTextFile.AtEndOfStream
    strNextLine = objTextFile.Readline
       intLineFinder = InStr(LCase(strNextLine), "popserver")
    If intLineFinder <> 0 Then
        strNextLine = "PopServer=NO_Check_Mail"
    End If
    strNewFile = strNewFile & strNextLine & vbCrLf
Loop
objTextFile.Close
Set objTextFile = objFSO.OpenTextFile("c:\temp\test.ini", Writing)
objTextFile.WriteLine strNewFile
objTextFile.Close





Const Read = 1
Const Write = 2
Set objFSOini = CreateObject("Scripting.FileSystemObject")
Set objTextFileini = objFSOini.OpenTextFile("c:\temp\test.ini", Read)
Do Until objTextFileini.AtEndOfStream
    strNextLine = objTextFileini.Readline
intLineFinder = InStr(LCase(strNextLine), "popaccount")
    If intLineFinder <> 0 Then
        strNextLine = "POPAccount=NO_Mail-pop"
    End If
    strNewFile = strNewFile & strNextLine & vbCrLf
Loop
objTextFileini.Close
Set objTextFileini = objFSOini.OpenTextFile("c:\temp\test.ini", Write)
objTextFileini.WriteLine strNewFile
objTextFileini.Close





Const Read = 1
Const Write = 2
Set objFSOini = CreateObject("Scripting.FileSystemObject")
Set objTextFileini = objFSOini.OpenTextFile("c:\temp\test.ini", Read)
Do Until objTextFileini.AtEndOfStream
    strNextLine = objTextFileini.Readline
intLineFinder = InStr(LCase(strNextLine), "checkformailevery")
    If intLineFinder <> 0 Then
        strNextLine = "CheckForMailEvery=0"
    End If
    strNewFile = strNewFile & strNextLine & vbCrLf
Loop
objTextFileini.Close
Set objTextFileini = objFSOini.OpenTextFile("c:\temp\test.ini", Write)
objTextFileini.WriteLine strNewFile
objTextFileini.Close



Thanks for any help or pointers.
0
Comment
Question by:bt707
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 4
9 Comments
 
LVL 67

Expert Comment

by:sirbounty
ID: 22698254
How about this?


Dim objFSO : Set objFSO = CreateObject("Scripting.FileSystemObject")
Dim objOut : Set objOut = objFSO.CreateTextFile("c:\newfile.init")
 
NewPopServer="NO_Check_Mail"
NewPOPAccount="NO_Mail-pop"
NewCheckMail=0
 
arrData = Split(objFSO.OpenTextFile("C:\original.ini").ReadAll, vbNewLine)
 
For each strData in arrData
  If Instr(strData, "popserver=") > 0 Then 
    NewData = NewData & "popserver=" & NewPopServer & vbNewLine
  ElseIf Instr(strData, "popaccount=") > 0 Then
    NewData = NewData & "popaccount=" & NewPopAccount & vbNewLine
  ElseIf instr(strData, "checkformailevery=") > 0 Then
    NewData = NewData & "checkformailevery=" & NewCheckMail & vbNewLine
  Else
    NewData = NewData & strData & vbNewLine
  End If
Next
 
objOut.WriteLine NewData
objOut.Close

Open in new window

0
 

Author Comment

by:bt707
ID: 22698298
sirbounty,

I get a error on line 8

Line 8
Char 1

Also, I have this in a script that does a lot of different things and don't want to create a new output file, will this work as well if I just do a inplace edit (or recreate the file the way I was doing it)

Thanks for you help,
0
 
LVL 67

Expert Comment

by:sirbounty
ID: 22698310
What's the error?
Yes, it will work for replacing the file, you'll just need to delay the 'creation' (or recreation).
Presumably you changed the location of the file to be read?
Try this version to replace the file:
Dim objFSO : Set objFSO = CreateObject("Scripting.FileSystemObject")
INIFile = "C:\Test\file.init" 
NewPopServer="NO_Check_Mail"
NewPOPAccount="NO_Mail-pop"
NewCheckMail=0
 
arrData = Split(objFSO.OpenTextFile(INIFile).ReadAll, vbNewLine)
 
For each strData in arrData
  If Instr(strData, "popserver=") > 0 Then 
    NewData = NewData & "popserver=" & NewPopServer & vbNewLine
  ElseIf Instr(strData, "popaccount=") > 0 Then
    NewData = NewData & "popaccount=" & NewPopAccount & vbNewLine
  ElseIf instr(strData, "checkformailevery=") > 0 Then
    NewData = NewData & "checkformailevery=" & NewCheckMail & vbNewLine
  Else
    NewData = NewData & strData & vbNewLine
  End If
Next
 
Dim objOut : Set objOut = objFSO.CreateTextFile(INIFile) 
objOut.WriteLine NewData
objOut.Close

Open in new window

0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:bt707
ID: 22698343
Sorry, I did not past the error, this is the error I get, now on line 7

Line 7
Char 1

error: Input past end of file

Looks like it's this line
arrData = Split(objFSO.OpenTextFile(INIFile).ReadAll, vbNewLine)

Thanks,
0
 
LVL 67

Expert Comment

by:sirbounty
ID: 22698350
Odd - try this in place of that line:
arrData = Split(Trim(objFSO.OpenTextFile(INIFile).ReadAll), vbNewLine)
0
 

Author Comment

by:bt707
ID: 22698366
I get the same error

error: Input past end of file

Not sure what's causing that.
0
 
LVL 67

Accepted Solution

by:
sirbounty earned 500 total points
ID: 22698399
Nor I, but we can try it from a different approach:

Let me know how you make out...
Dim objFSO : Set objFSO = CreateObject("Scripting.FileSystemObject")
INIFile = "C:\Test\file.init" 
NewPopServer="NO_Check_Mail"
NewPOPAccount="NO_Mail-pop"
NewCheckMail=0
 
Dim objInput : Set objInput =objFSO.OpenTextFile(INIFile)
 
Do While Not objInput.AtEndOfStream
  strData = objInput.ReadLine 
  If Instr(strData, "popserver=") > 0 Then 
    NewData = NewData & "popserver=" & NewPopServer & vbNewLine
  ElseIf Instr(strData, "popaccount=") > 0 Then
    NewData = NewData & "popaccount=" & NewPopAccount & vbNewLine
  ElseIf instr(strData, "checkformailevery=") > 0 Then
    NewData = NewData & "checkformailevery=" & NewCheckMail & vbNewLine
  Else
    NewData = NewData & strData & vbNewLine
  End If
Loop
 
Dim objOut : Set objOut = objFSO.CreateTextFile(INIFile) 
objOut.WriteLine NewData
objOut.Close

Open in new window

0
 

Author Closing Comment

by:bt707
ID: 31505474
sirbounty,

No more errors, it did not edit but I found out why, the case in the file is dffierent than what I put in.
I had fixed that part in my original script so did not notice that. Problem way what I posted and did not note that.

It's working now for what I wanted to do, so I don't have to open and edit the file 3 times.

Very nice help here,

Thank you very much!!!
0
 
LVL 67

Expert Comment

by:sirbounty
ID: 22698467
Glad I could help  - thanx for the grade! :^)
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

I met Paul Devereux (@pdevereux) today when I responded to his tweet asking “Anybody know how to automate adding files from disk to a folder in #outlook  ?”.  I replied back and told Paul that using automation, in this case scripting, to add files t…
When it comes to writing scripts for a Client/Server computing environment it is essential to consider some way of enabling the authentication functionality within a script. This sort of consideration mainly comes into the picture when we are dealin…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…
Suggested Courses

738 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question