Page 1 of 1 1
Topic Options
#146876 - 2005-08-31 10:37 AM Replace Txt in middle of a string...
dyoung Offline
Lurker

Registered: 2005-08-31
Posts: 2
Morning all,

Can someone point me in the direction of how i would replace some text in the middle of a path string..

Eg.. i have a path of

c:\windows\system32;c:\whatever;k:\ora81;c:\stuff

and i want to replace the k:\ora81 with k:\ora92 but keep the path data around this string the same..?

Thanks

Top
#146877 - 2005-08-31 11:03 AM Re: Replace Txt in middle of a string...
Mart Moderator Offline
KiX Supporter
*****

Registered: 2002-03-27
Posts: 4667
Loc: The Netherlands
How about this UDF?
PathMod() Modify System Variable
_________________________
Mart

- Chuck Norris once sold ebay to ebay on ebay.

Top
#146878 - 2005-08-31 11:14 AM Re: Replace Txt in middle of a string...
Mart Moderator Offline
KiX Supporter
*****

Registered: 2002-03-27
Posts: 4667
Loc: The Netherlands
Got a spare silver platter so here we go....
Well actually its not my silver platter. The code is taken from the example. I just replaced the values to replace with the ones suitable for you.

Code:

$CValue = "k:\ora81"
$RValue = "k:\ora92"
$RC = PathMod($CValue, $RValue)
;
;
; Modifies the System Enviornment Variable
; This replaces a specific value with another Value.
;
;Name : PathMod
;Author: WonderBoy
;Action: Modifies the System Enviornment Variable
;Date: 11/19/03
;syntax: PathMod($CValue,$RValue)
;Parameters: $CValue = path that you want to remove
; $RValue = path that you want to substitue
;
;EXAMPLE(s): $CValue = "c:\program files\Adobe"
; $RValue = "c:\Adobe"
; $RC = PathMod($CValue, $RValue)
; Returns:
; 0 Value was found and path changed
; 1 failed to write registry value
; 2 $CValue was not passed or empty
; 3 $RValue was not passed or empty
;
Function PathMod($CValue, $RValue)
Dim $Array, $aCntr, $KeyVal2, $fCntr, $stat, $KeyLoc, $Keynam
$aCntr = 0
$fCntr = 0
$stat = "FALSE"
$KeyLoc = "HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Session Manager\Environment"
$keyNam = "Path"

If $CValue = "" OR $RValue = ""
Select
Case $CValue = ""
$Pathmod = 2
Case $RValue = ""
$Pathmod = 3
EndSelect
Return
EndIf

$KeyVal = ReadValue ($KeyLoc, $KeyNam)
$Array = Split($KeyVal,";",-1)
For Each $Element In $Array
If $Element = $CValue
$Array[$acntr] = $RValue
$stat = "TRUE"
EndIf
$acntr = $acntr + 1
Next
For Each $Element In $Array
If $fCntr = 0
$KeyVal2 = $element
$fcntr = 1
Else
$keyVal2 = $KeyVal2 +";"+ $Element
$fcntr = $fcntr + 1
EndIf
Next
If $Stat = "TRUE"
$RC = WriteValue($Keyloc,$KeyNam,$KeyVal2,"REG_SZ")
If $RC = 0
$PathMod = 0
Return
Else
$Pathmod = 1
Return
EndIf
EndIf
EndFunction



Edited by Mart (2005-08-31 11:21 AM)

Top
#146879 - 2005-08-31 06:15 PM Re: Replace Txt in middle of a string...
Kdyer Offline
KiX Supporter
*****

Registered: 2001-01-03
Posts: 6241
Loc: Tigard, OR
BTW, there are a couple of Replace() UDFs out there too.

Kent
_________________________
Utilize these resources:
UDFs (Full List)
KiXtart FAQ & How to's

Top
#146880 - 2005-09-01 12:19 AM Re: Replace Txt in middle of a string...
Sealeopard Offline
KiX Master
*****

Registered: 2001-04-25
Posts: 11163
Loc: Boston, MA, USA
And even a SPLIT-JOIN would do.
_________________________
There are two types of vessels, submarines and targets.

Top
Page 1 of 1 1


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

Who's Online
0 registered and 304 anonymous users online.
Newest Members
Praveer, Ollero, rayyo, Foxlicht, DonChild
17742 Registered Users

Generated in 0.053 seconds in which 0.033 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