Page 2 of 2 <12
Topic Options
#158577 - 2006-03-09 01:53 AM Re: Change text string in multiple INI files
Shawn Administrator Offline
Administrator
*****

Registered: 1999-08-13
Posts: 8611
ja - like I said, dont know if this is to be run locally or remotely - just reading between the lines. Using that key would be very handy.
Top
#158578 - 2006-03-09 06:22 AM Re: Change text string in multiple INI files
NTDOC Administrator Offline
Administrator
*****

Registered: 2000-07-28
Posts: 11631
Loc: CA
Okay, here is some SILVER PLATTER code for you to run Rich.

Requires KiXtart 4.5 or newer, and does not support NT4.
Should work fine on 2000/XP/2003 systems.

Please read the notes within the code on how to use it, and post any questions or comments you have.

Normally one would place the $sComputer parameter first in the UDF, however due to the way that Optional works
in KiXtart anything after the word Optional becomes optional so I placed the $sComputer at the end since
it is the only optional parameter depending on how you run it.
If you run it against a lot of computers then it's not optional really, but if you run it while locally
logged on then it's optional and does not need to be supplied.


Break On
Dim $SO
$SO=SetOption('Explicit','On')
$SO=SetOption('NoVarsInStrings','On')
$SO=SetOption('NoMacrosInStrings','On')

Dim $Computer, $Computers, $Update, $Ping
$Computers = ReadFile(@ScriptDir+'\'+'computers.txt')
If @ERROR
'There was an error reading the computers.txt file: ' + @ERROR + ' - ' + @SERROR ?
Quit 1
EndIf
For Each $Computer In $Computers
If $Computer
$Computer = Trim($Computer)
$Ping = OSPing($Computer)
If $Ping
$Update = UpdateRemoteINI('main.ini','Finance','Member',5,$Computer)
;Create a list of computers you want to update with one computer per line
;and save it in the same folder as this script named computers.txt
;Edit the line above to indicate the correct SECTION, KEY, and STRING
;Per the WriteProfileString function in the manual.
Else
'The system ' + $Computer + ' did not respond. ' ?
EndIf
EndIf
Next

; If you want to do a single computer while you're logged in you can run something like this
; $Update = UpdateRemoteINI('main.ini','Finance','Member',5)

; There is no need to supply the name of the computer if you run it while logged in locally
; with Admin rights. You need Admin rights to write to all those other profiles.


Function UpdateRemoteINI($File,$Section,$Key,$String,Optional $sComputer)
Dim $BaseKey, $BaseFolder,$Drive,$Folder,$Profile,$W
$BaseKey = 'HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion\'
$sComputer=IIf(Not $sComputer,'','\\'+Join(Split($sComputer,'\'),'',3)+'\')
If Not $sComputer
$BaseFolder = ExpandEnvironmentVars(ReadValue($BaseKey+'ProfileList','ProfilesDirectory'))
Else
$BaseFolder = ReadValue($sComputer+$BaseKey+'ProfileList','ProfilesDirectory')
$Drive = InStrRev($BaseFolder,'\')
$BaseFolder = $sComputer+Left(ReadValue($sComputer+$BaseKey,'SystemRoot'),1)+'$\'+Right($BaseFolder,-$Drive)
EndIf
$Folder = Dir($BaseFolder)
While @ERROR = 0
$Folder = $BaseFolder + '\' + $Folder
If GetFileAttr($Folder) & 16
$Profile = $Folder + '\' + $File
If Exist($Profile)
'Updated ' + $File + ' file for: ' + $Profile ?
$W = WriteProfileString($Profile, $Section, $Key,$String)
EndIf
EndIf
$Folder = Dir()
Loop
Exit 1
EndFunction

Function ReadFile($file)
Dim $lf, $f, $_, $t
$lf=CHR(10)
$f=FreeFileHandle
$_=Open($f,$file)
If @ERROR Exit @ERROR EndIf
Do $t=$t+$lf+ReadLine($f) Until @ERROR
$_=Close($f)
$ReadFile=Split(SubStr($t,2),$lf)
EndFunction

Function OSPing($sComputer)
Shell '"'+%COMSPEC%+'" /c ping -n 1 '+$sComputer+' |find /C "TTL=" >nul'
$OSPing = NOT @ERROR
EndFunction

 

 
 

Top
#158579 - 2006-03-09 08:26 PM Re: Change text string in multiple INI files
rich100 Offline
Lurker

Registered: 2006-03-08
Posts: 3
Shawn, My fault for not being more descriptive. We need to update about 200 clients with the new settings to the INI file. We evealuated dumping a new INI file down to each client but the file contains tons of user specific settings. Some PC's are shared and thus have multiple profiles we need to update. It's a small fraction of the overall number of client's. We figured running it upon login would work fine. If the INI file does not exist, it won't do anything on the clients PC (not real efficient but gets the job done.) Since we know the clients we need to update, NTDOC's solution may actually work better.

Eventually I'd like to look at reading a remote registry in order to perform this and other functions, especially on the shared PC's.

Thanks.
Rich

Top
#158580 - 2006-03-09 08:31 PM Re: Change text string in multiple INI files
NTDOC Administrator Offline
Administrator
*****

Registered: 2000-07-28
Posts: 11631
Loc: CA
Rich,

The code I posted above will work both locally or remotely.
It will still attempt to update all profiles but if the current user does not have Admin rights then the only one it will update is his/her own profile .ini file.
 
The good part about reading the registry is that it will work correctly regardless of what drive the system was installed on.

Granted that for most if not all systems it would be on the C: drive, but if for some reason it was on E: or F: etc.. then the other method would fail.

Top
#158581 - 2006-03-09 08:42 PM Re: Change text string in multiple INI files
Les Offline
KiX Master
*****

Registered: 2001-06-11
Posts: 12734
Loc: fortfrances.on.ca
Actually DOC, not true.
$BaseFolder = ExpandEnvironmentVars(ReadValue($BaseKey+'ProfileList','ProfilesDirectory'))
That line will resolve the enviro var an the PC the script is running from, not against.
You need to Split() off %SystemDrive% and determine that with separate code. Sorry, I should have mentioned that before.
_________________________
Give a man a fish and he will be back for more. Slap him with a fish and he will go away forever.

Top
#158582 - 2006-03-09 09:33 PM Re: Change text string in multiple INI files
NTDOC Administrator Offline
Administrator
*****

Registered: 2000-07-28
Posts: 11631
Loc: CA
True, but come on. I can pretty much guarantee you that if %SystemDrive% or %SystemRoot% or %windir% or %ProgramFiles% are NOT resolving on your local system then you will be having all types of programs not functioning correctly.

However, that given if Rich is intent on running this locally and wants me to modify per your suggestion I will, but if he has no intention of really running the code then I don't have time to bother with the change.

Nice catch though Les.
 


Edited by NTDOC (2006-03-09 09:34 PM)

Top
Page 2 of 2 <12


Moderator:  Jochen, Allen, Radimus, Glenn Barnas, ShaneEP, Ruud van Velsen, Arend_, Mart 
Hop to:
Shout Box

Who's Online
0 registered and 611 anonymous users online.
Newest Members
Sir_Barrington, batdk82, StuTheCoder, M_Moore, BeeEm
17886 Registered Users

Generated in 0.072 seconds in which 0.026 seconds were spent on a total of 12 queries. Zlib compression enabled.

Search the board with:
superb Board Search
or try with google:
Google
Web kixtart.org