Page 1 of 1 1
Topic Options
#203891 - 2011-12-07 12:34 AM get next line
booey Offline
Getting the hang of it

Registered: 2005-07-25
Posts: 76
Loc: USA
I have a script that I'm working on where I need to find a line, write that line to a file and then get the following line and append it to the same line. If the first string of the line has a K in it, look for the following line.

The file layout is in the attached file.

The end result is a line that looks like this:
ABCD123 * DOE,JOHN J (61) 05/17/11 05/17/11 06/03/11 08/05/11 63 81.38 20.70 60.68 0.00 25.44

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

$file = "C:\temp\file.txt"
$handle = FreeFileHandle()

$sitearray = loadfile($file,"@crlf")

	For Each $line In $sitearray
	;	$line
		
		$field = Split($line," ")
		$field[0] ?
		If InSTR($field[0],"K")
			$field[0]

			Else
			"No K in string" ?
		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




I'm stuck at how to get the line following the K line and append lines 1 and 2 (for lack of a better term) together. Any help is appreciated.


Attachments
Document2.txt (121 downloads)
Description:



Top
#203892 - 2011-12-07 12:54 AM Re: get next line [Re: booey]
ShaneEP Moderator Offline
MM club member
*****

Registered: 2002-11-29
Posts: 2127
Loc: Tulsa, OK
Here's my attempt...Just change the file constants.

 Code:
$SO = SETOPTION("NoMacrosInStrings", "ON")
$SO = SETOPTION("NoVarsInStrings", "ON")
BREAK ON

$file = @ScriptDir+"\file.txt"
$newfile = @ScriptDir+"\file2.txt"
$handle = FreeFileHandle()

$sitearray = loadfile($file,@crlf)

If Open($handle,$newfile,5)=0 AND Ubound($sitearray)>=0
   For $x=0 to UBound($sitearray)
      $field = Split($sitearray[$x]," ")[0]
      If InSTR($field,"K") AND Len($field)
         $newline = $sitearray[$x]+" "+$sitearray[$x+1]
         $nul = WriteLine($handle,$newline+@CRLF)
      EndIf
   Next
   $nul = Close($handle)
Endif

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

Top
#203893 - 2011-12-07 03:44 PM Re: get next line [Re: ShaneEP]
booey Offline
Getting the hang of it

Registered: 2005-07-25
Posts: 76
Loc: USA
That worked great. Thanks for the help.
Top
Page 1 of 1 1


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

Who's Online
0 registered and 1441 anonymous users online.
Newest Members
batdk82, StuTheCoder, M_Moore, BeeEm, min_seow
17885 Registered Users

Generated in 0.052 seconds in which 0.024 seconds were spent on a total of 14 queries. Zlib compression enabled.

Search the board with:
superb Board Search
or try with google:
Google
Web kixtart.org