dyoung
(Lurker)
2005-08-31 10:37 AM
Replace Txt in middle of a string...

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


Mart
(KiX Supporter)
2005-08-31 11:03 AM
Re: Replace Txt in middle of a string...

How about this UDF?
PathMod() Modify System Variable


Mart
(KiX Supporter)
2005-08-31 11:14 AM
Re: Replace Txt in middle of a string...

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



Kdyer
(KiX Supporter)
2005-08-31 06:15 PM
Re: Replace Txt in middle of a string...

BTW, there are a couple of Replace() UDFs out there too.

Kent


Sealeopard
(KiX Master)
2005-09-01 12:19 AM
Re: Replace Txt in middle of a string...

And even a SPLIT-JOIN would do.