I have a request to go through several files and essentially do a find and replace. The "found" value will then replace a value that is is the file. In the sample data below, the "Status" value (ABCD, in this example) needs to be taken and replace the "Place" value of XXXX with ABCD. The Status and Place values will vary from file to file as in relates to line number, but they will always be four characters and be listed with the line that starts Place: and Status:. Below is the code I have so far. I've been able to get the place and status values, but I'm not sure how to replace the value in Place with the Status value that I have set in the variables and then save the entire document back in the original format. I've also attached a file that I'm using to test with. Any help is appreciated.

Here's the code I have so far
 Code:
$SO=SETOPTION("Explicit", "ON")
$SO=SETOPTION("NoMacrosInStrings", "ON")
$SO=SETOPTION("NoVarsInStrings", "ON")
BREAK ON

DIM $file
DIM $handle
DIM $read
DIM $line
DIM $c
DIM $M
DIM $sub
DIM $cdetail
DIM $ccode
DIM $mdetail
DIM $mcode


$file = "C:\temp\m\m.txt"


$file = loadfile($file,@CRLF)

For Each $line In $file 

If $line <> 0		
	
If InSTR($line,"Place:") <> 0

	$line ?
	$c = Split($line,":")
	$cdetail = Split($c[1]," ")

		
;$ccode = SubSTR($c[1],14,4)

"p detail: " + $cdetail[14] ?


	Else


EndIf

If InSTR($line,"CODE:") <> 0
	
	$line ?
	$m = Split($line,":")
	$mdetail = Split($m[1]," ")
	
;$mcode = SubSTR($m[1],15,4)
"m detail: " + $mdetail[15] ?


	Else
EndIf


	
EndIf
Next

;Functionction	LoadFile()
;
;Author		Bryce Lindsay Bryce@isorg.net
;
;Action		Loads a file in to a variable or an array.
;
;Syntax		LoadFile("Filename",[Array Delim], [UNI/ANSI file type])
;
;Version	1.1
;
;Date Revised	9:14 AM 8/15/2006
;
;Parameters	Filename
;		name of the file that you want to load.
;
;		Optional Array
;		set this option if you want to return the 
;		information as an array split on the given value
;
;		Unicode/ANSI file type
;		3 = force file open as ANSI
;		2 = forse file open as UNI
;		1 = Opens the file using the system default. (the UDF will use this as default)
;
;Remarks		finaly made this into a UDF and posted it... got tired 
;		of having to hunt it down.
;
;Returns		if the array flag is not set it returns a variable 
;		containing the contents of the file.
;
;		if the array flag is set, it will return an array 
;		of the file split on the value of the given variable
;
;Dependencies	Scripting.FileSystemObject
;
;KiXtart Ver	4.51
; 
;Example(s)	
;		;load the file test.txt into a variable
;		$Data=loadfile('test.txt')
;		? $data 
;
;		;load the file "test.txt" into an array split on @crlf
;		$data = loadfile('test.txt',@crlf)
;		for each $line in $data
;			? $line
;		next
;*/
Function loadfile($file, optional $array, $Uni)
	DIM $fso,$f,$fs
	if $uni $uni = $uni-3 else $uni = -2 endif

	if not $uni $uni = -2 endif
	$fso = CreateObject("Scripting.FileSystemObject")
	$f = $fso.GetFile($file)
	If @ERROR Exit 2 EndIf
	$fs = $f.OpenAsTextStream(1,$uni)
	if not $array
		$loadfile = $fs.Read($f.size)
	else
		$loadfile = Split($fs.Read($f.size),$array)
	endif
	Exit @ERROR 
EndFunction


Attachments
m.txt (144 downloads)
Description: