Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Change text in line three

94 views
Skip to first unread message

Daniel Neuhaus

unread,
Sep 11, 2002, 2:24:13 AM9/11/02
to
I have to change the whole 3. line in a ini-file. It should check the IP of
the local machine (because DHCP) and write the adress in that ini-file after
that it schould run the application. I begun with a script but im
unexperienced.
----------------------------------------------------------
Dim WSHShell, ip, fso, f
Set WSHShell = WScript.CreateObject("WScript.Shell")
Const ForReading = 1, ForWriting = 2, ForAppending = 3
Set fso = CreateObject("Scripting.FileSystemObject")
Set f = fso.OpenTextFile("c:\apps\WindowsEnabler\winserv.ini", ForWriting,
True)

ip =
WshShell.RegRead("HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\MDGMP
ORT1\Parameters\Tcpip\DhcpIPAddress")

f.WriteLine (ip)

wshshell.run "C:\apps\WindowsEnabler\winserv.exe", 1, true

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

thanks


Torgeir Bakken

unread,
Sep 11, 2002, 2:46:34 PM9/11/02
to
Daniel Neuhaus wrote:

> I have to change the whole 3. line in a ini-file. It should check the IP of
> the local machine (because DHCP) and write the adress in that ini-file after
> that it schould run the application. I begun with a script but im
> unexperienced.

Hi

Here are two functions, ReadIni and WriteIni. You can use WriteIni to update
your ini-file value.

Note: It is using FSO, so it should not be used against ini files that can be
updated simultaneous by another process.

Example:

' -------------------- Code Start -----------------------

sFileName = "c:\data\ExchInst.ini"

sSection = "gfn_mid microsoft exchange 2000"
sKey = "599AD9B5-707B-4CC1-A4E3-24614A619F2B"
sData = "MIGRATION\AdminUser"

' update the ini file
WriteIni sFileName, sSection, sKey, sData

' read the data back
sUser = ReadIni(sFileName, sSection, sKey)

WScript.Echo sUser


' ===================================
' ================= FUNCTIONS & SUBS
' ===================================
'~~Author~~. ANTOINE Jean-Luc
'~~Comment~~.
' This script was stolen from the discussion lounge. Its a Function
' that imitates the WriteProfileString API so that you can edit
' INI's from Vbscript

'*** Modified by Torgeir Bakken 2001.06.26
'*** -> no brackets in section names
'*** -> to not return leading =
'*** -> to be case insensitiv
'*** -> fix for white space in section part
'*** -> fix for white space in key part
'*** -> check if folder exist before trying to write
'~~Script~~.

' READINI ( file, section, item )
' file = path and name of ini file
' section = [Section] must not be in brackets
' item = the variable to read

Function ReadIni(file, section, item)

Set oFSO = CreateObject("Scripting.FileSystemObject")
ReadIni = ""
file = Trim(file)
item = Trim(item)
If oFSO.FileExists( file ) Then
Set ini = oFSO.OpenTextFile( file, 1, False)

Do While ini.AtEndOfStream = False
line = ini.ReadLine
line = Trim(line)
If LCase(line) = "[" & LCase(section) & "]" Then
line = ini.ReadLine
line = Trim(line)
Do While Left( line, 1) <> "["
'If InStr( 1, line, item & "=", 1) = 1 Then
equalpos = InStr(1, line, "=", 1 )
If equalpos > 0 Then
leftstring = Left(line, equalpos - 1 )
leftstring = Trim(leftstring)
If LCase(leftstring) = LCase(item) Then
ReadIni = Mid( line, equalpos + 1 )
ReadIni = Trim(ReadIni)
Exit Do
End If
End If

If ini.AtEndOfStream Then Exit Do
line = ini.ReadLine
line = Trim(line)
Loop
Exit Do
End If
Loop
ini.Close
End If
End Function


' WRITEINI ( file, section, item, myvalue )
' file = path and name of ini file
' section = [Section] must not be in brackets
' item = the variable to write;
' myvalue = the myvalue to assign to the item.
'
Sub WriteIni( file, section, item, myvalue )

Set oFSO = CreateObject("Scripting.FileSystemObject")
path = Mid( file, 1, InStrRev( file, "\" ) )
' Check if path to file exists, quit if not so
If Not oFSO.FolderExists ( path ) Then
MsgBox "Error: WriteIni failed, folder path to ini file " _
& file & " not found!"
Wscript.Quit
End If

in_section = False
section_exists = False
item_exists = ( ReadIni( file, section, item ) <> "" )
wrote = False
file = Trim(file)
itemtrimmed = Trim(item)
myvalue = Trim(myvalue)

Set read_ini = oFSO.OpenTextFile( file, 1, True, TristateFalse )
Set write_ini = oFSO.CreateTextFile( path & "temp_ini.ini", False)

While read_ini.AtEndOfStream = False
line = read_ini.ReadLine
linetrimmed = Trim(line)
If wrote = False Then
If LCase(line) = "[" & LCase(section) & "]" Then
section_exists = True
in_section = True

ElseIf InStr( line, "[" ) = 1 Then
in_section = False
End If
End If

If in_section Then
If item_exists = False Then
write_ini.WriteLine line
write_ini.WriteLine item & "=" & myvalue
wrote = True
in_section = False

Else
equalpos = InStr(1, line, "=", 1 )
If equalpos > 0 Then
leftstring = Left(line, equalpos - 1 )
leftstring = Trim(leftstring)
If LCase(leftstring) = LCase(item) Then
write_ini.WriteLine itemtrimmed & "=" & myvalue
wrote = True
in_section = False
End If
End If
If Not wrote Then
write_ini.WriteLine line
End If
End If
Else
write_ini.WriteLine line
End If
Wend

If section_exists = False Then ' section doesn't exist
write_ini.WriteLine
write_ini.WriteLine "[" & section & "]"
write_ini.WriteLine itemtrimmed & "=" & myvalue
End If

read_ini.Close
write_ini.Close
oFSO.DeleteFile file
oFSO.MoveFile path & "temp_ini.ini", file
End Sub
' -------------------- Code Stop -----------------------


--
torgeir


Daniel Neuhaus

unread,
Sep 12, 2002, 1:45:54 AM9/12/02
to
thanks a lot i will try it out
daniel

"Torgeir Bakken" <Torgeir.B...@hydro.com> schrieb im Newsbeitrag
news:3D7F8F89...@hydro.com...

0 new messages