Page 1 of 1 1
Topic Options
#155431 - 2006-01-16 07:48 PM FIND "String" in a text file, and REPLACE with another "String"
dataspike Offline
Getting the hang of it

Registered: 2005-03-09
Posts: 89
Loc: San Diego, CA
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

Top
#155432 - 2006-01-16 08:12 PM Re: FIND "String" in a text file, and REPLACE with another "String"
Howard Bullock Offline
KiX Supporter
*****

Registered: 2000-09-15
Posts: 5809
Loc: Harrisburg, PA USA
You could simply use WriteProfileString. See the docs.
_________________________
Home page: http://www.kixhelp.com/hb/

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

Registered: 2005-03-09
Posts: 89
Loc: San Diego, CA
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


Top
#155434 - 2006-01-16 09:25 PM Re: FIND "String" in a text file, and REPLACE with another "String"
Lonkero Administrator Offline
KiX Master Guru
*****

Registered: 2001-06-05
Posts: 22346
Loc: OK
you can avoid the iffing on the entry and value by using left(,-1) before splitting
_________________________
!

download KiXnet

Top
#155435 - 2006-01-16 10:00 PM Re: FIND "String" in a text file, and REPLACE with another "String"
Howard Bullock Offline
KiX Supporter
*****

Registered: 2000-09-15
Posts: 5809
Loc: Harrisburg, PA USA
Why are you reading each line of the INI file? Don't you know the section and value name to update it directly?
_________________________
Home page: http://www.kixhelp.com/hb/

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

Registered: 2005-03-09
Posts: 89
Loc: San Diego, CA
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?


Edited by dataspike (2006-01-16 10:59 PM)

Top
#155437 - 2006-01-16 11:04 PM Re: FIND "String" in a text file, and REPLACE with another "String"
NTDOC Administrator Offline
Administrator
*****

Registered: 2000-07-28
Posts: 11623
Loc: CA
If $sValue=$sOldString1 Or $sValue=$sOldString2
Top
#155438 - 2006-01-16 11:40 PM Re: FIND "String" in a text file, and REPLACE with another "String"
dataspike Offline
Getting the hang of it

Registered: 2005-03-09
Posts: 89
Loc: San Diego, CA
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"


Edited by dataspike (2006-01-16 11:42 PM)

Top
#155439 - 2006-01-17 09:06 AM Re: FIND "String" in a text file, and REPLACE with another "String"
Lonkero Administrator Offline
KiX Master Guru
*****

Registered: 2001-06-05
Posts: 22346
Loc: OK
and that breaks the whole functionality as it would always return true.
_________________________
!

download KiXnet

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

Registered: 2005-03-09
Posts: 89
Loc: San Diego, CA
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.
Top
Page 1 of 1 1


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

Who's Online
0 registered and 248 anonymous users online.
Newest Members
gespanntleuchten, DaveatAdvanced, Paulo_Alves, UsTaaa, xxJJxx
17864 Registered Users

Generated in 0.061 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