dataspike
(Getting the hang of it)
2006-01-16 07:48 PM
FIND "String" in a text file, and REPLACE with another "String"

I have Citrix installed on all my workstations and I have a particular application that I am moving to a new server, with a new "Address", so I want to change the address in the .INI file on all the workstations.

I found the ReadFile UDF, and thought I could try that, but I wasn't sure how to work it. I also thought I'd see if anyone else has already to tried to accomplish this?

Quote:

;Function	ReadFile()  
;
;Author Radimus
;
;Contributors Lonky... This is basically his code that I trimmed down to its
; simplest function
;
;Action Reads a file into an array
;
;Syntax $array=ReadFile($file)
;
;Version 1.0.1
;
;Date 10/21/2003
;
;Date Revised 10-22-2003 - dimmed vars
;
;Parameters file
; source filename
;
;Remarks Pair this with WriteFile() to read and write an entire file easily
;
;Returns @error if failed
;
;Dependencies None
;
;KiXtart Ver 4.02
;
;Example(s) $array=ReadFile('c:\file.txt')

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


[ 22. October 2003, 11:26: Message edited by: Radimus ]




Any help/ideas would be awesome.

Thanks,
Chris


Howard Bullock
(KiX Supporter)
2006-01-16 08:12 PM
Re: FIND "String" in a text file, and REPLACE with another "String"

You could simply use WriteProfileString. See the docs.

dataspike
(Getting the hang of it)
2006-01-16 09:19 PM
Re: FIND "String" in a text file, and REPLACE with another "String"

Quote:

You could simply use WriteProfileString. See the docs.



Thanks for pointing me in the right direction.

Code:

$sMyIni = @SCRIPTDIR + "\appsrv.ini"
$sOldString="old-citrix.olddomain.com"
$sNewString="new-citrix.newdomain.com"

For Each $sSection in Split(ReadProfileString($sMyIni,"",""),Chr(10))
If $sSection
"Checking section "+$sSection+@CRLF
For Each $sEntry in Split(ReadProfileString($sMyIni,$sSection,""),Chr(10))
If $sEntry
$sValue=ReadProfileString($sMyIni,$sSection,$sEntry)
"Entry "+$sEntry+" has value "+$sValue+" status:"
If $sValue=$sOldString
" **UPDATED**"+@CRLF
$=WriteProfileString($sMyIni,$sSection,$sEntry,$sNewString)
Else
" No change"+@CRLF
EndIf
EndIf
Next
EndIf
Next



LonkeroAdministrator
(KiX Master Guru)
2006-01-16 09:25 PM
Re: FIND "String" in a text file, and REPLACE with another "String"

you can avoid the iffing on the entry and value by using left(,-1) before splitting

Howard Bullock
(KiX Supporter)
2006-01-16 10:00 PM
Re: FIND "String" in a text file, and REPLACE with another "String"

Why are you reading each line of the INI file? Don't you know the section and value name to update it directly?

dataspike
(Getting the hang of it)
2006-01-16 10:58 PM
Re: FIND "String" in a text file, and REPLACE with another "String"

The section name can vary.

Some are called JDE, some are called JDEdwards, and so on.

I actually grabbed the above code from another thread using the WriteProfileString command. If anyone has a better solution I would appreciate it.

The other thing that I am running into is that, I have found a couple instances where the client is using the FQDN (server.domain.com) where others are just using the server name. So it's skipping one or the other.

Any ideas how to parse multiple variables for the "sOldString" variable?


NTDOCAdministrator
(KiX Master)
2006-01-16 11:04 PM
Re: FIND "String" in a text file, and REPLACE with another "String"

If $sValue=$sOldString1 Or $sValue=$sOldString2

dataspike
(Getting the hang of it)
2006-01-16 11:40 PM
Re: FIND "String" in a text file, and REPLACE with another "String"

Quote:

If $sValue=$sOldString1 Or $sValue=$sOldString2



That's what I thought, but wasn't sure with a For command and I was only doing

If $sValue = $sOldString or "othervariable"


LonkeroAdministrator
(KiX Master Guru)
2006-01-17 09:06 AM
Re: FIND "String" in a text file, and REPLACE with another "String"

and that breaks the whole functionality as it would always return true.

dataspike
(Getting the hang of it)
2006-01-17 07:32 PM
Re: FIND "String" in a text file, and REPLACE with another "String"

Yup... exactly. It screwed up the entire INI file. But NTDOC's code worked exactly as needed. Thanks for all the help guys. I need to get back to coding, I've lost a lot of my logical thought processes.