Page 1 of 1 1
Topic Options
#77360 - 2003-11-12 01:10 AM Array
PhoeniX Offline
Fresh Scripter

Registered: 2000-11-01
Posts: 22
Loc: Brisbane, QLD, Australia
Hey all,

I am trying to use an array to log data,
the data is too long for a string. I have the array sample it seems to read by the counter, but I only get one line written to the file.

Thanking you in Advance
Phoe.

code:
  

$array=ReadFile('c:\kix\archlist.txt')
$=WriteFile('c:\kix\file.txt',$array)

Function ReadFile($file)
Dim $lf, $f, $_, $t ,$x
$x = 0
$lf=chr(10)
$f=freefilehandle
$_=open($f,$file)
if @error exit @error endif
do $t=$t+$lf+readline($f) $x=$x + 1 until @error
$_=close($f)
$ReadFile=split(substr($t,2),$lf)
$x
EndFunction


Function WriteFile($file,$array)
Dim $f, $line,$y
If Not VarType($Array) & 8192 Exit(1) EndIf
$y = 0
$f=freefilehandle
$=open($f,$file,5)
if @error exit @error endif
for each $line in $array
$y=$y + 1
$=writeline($f,$line+@crlf)
if @error exit @error endif
$=close($f)
??$y
EndFunction


Top
#77361 - 2003-11-12 01:14 AM Re: Array
Howard Bullock Offline
KiX Supporter
*****

Registered: 2000-09-15
Posts: 5809
Loc: Harrisburg, PA USA
In your WriteFile() function you do not have a NEXT to increment yopur "For each" loop. Then you immediately close the output file.

Strings are not limited to 32K as the docs states. You can put 400MB of data into a string variable.
_________________________
Home page: http://www.kixhelp.com/hb/

Top
#77362 - 2003-11-12 02:25 AM Re: Array
PhoeniX Offline
Fresh Scripter

Registered: 2000-11-01
Posts: 22
Loc: Brisbane, QLD, Australia
Thanks Howard,

that solved it, but the main script with a long string, built of directory names fails when I try to write it to a log.

Error: expression to long
Line: 128

Which is: $string1 = substr("$string",1,$spot-1)

If I have it collect less directories it works.

Complete code.

code:
 

; 15th August 2003
; Client File Archiver
;
;
cls break on

; Adjust ip for your office client directory
; use "X:" "\\10.1.2.20\root\"

$file = "c:\kix\archlist.txt" ; Input file. Relies on input file of archlist.txt with a single 7 digit number per line.
$ziparchdir = "c:\kix\zip" ; Directory to place ziped files
$zipbasedir= "c:kix\clients\" ; Directory where all client directories exist
$archlog = "c:\kix\log.txt" ; Archive stats and log file.
$countf = 0


$ = open(1,$file)
$line = readline(1)

;start with an initial array size of 5
dim $array[5]
$count = 0

while not @error

; let check to make sure you are not over the current array size,
; if we are increase the array size by 10

if $count = ubound($array)
redim preserve $array[ubound($array)+10]
endif

;add a line to the array
$array[$count] = $line

;increase our counter by 1
$count = $count + 1

;get the next line of text
$line = readline(1)

loop

$ = close(1)

;ok, time to trim back to array size to the actual size of the data.
if $count > 0
redim preserve $array[$count - 1]
else
;hmmm looks like no information was loaded... make sure your file
;is avaliable
$array = ""
endif

;just a simple check to show your data in the array...
for each $clientno in $array
; ? $clientno ?

if len($clientno) = 7
$dirno = $clientno

$dirfnd = dir("c:\kix\clients\"+$dirno+"*")

if ($dirfnd <> ".") And ($dirfnd <> "..") And len($dirfnd); > 7 And GetFileAttr($basedir+"\"+$name) & 16)
? "Directory found: $dirfnd" ?
$finds = $finds+ ", " + $dirfnd

;zip_dir
$filename = $dirfnd
; Create Zip
$cmd=" wzzip -a -ex -rp -m "
$cmd=$cmd+' "$zipbasedir\'+LTRIM(RTRIM($filename))+'.zip" "$zipbasedir\'+LTRIM(RTRIM($filename))+'" '
? $cmd
;SHELL '%comspec% /c $cmd '

; Move completed zip file to archive
$cmdmv=' move /y "$zipbasedir\*.zip" "$ziparchdir" '
;SHELL '%comspec% /c $cmdmv '

else
; ? "Directory not found: $dirno" ?
$nofinds = $nofinds+ ", " + $dirno
endif
endif
NEXT

? "Directories found = " $finds ?
? "Directories not found = " $nofinds ?

If Open( 3 , $archlog , 5 ) = 0

if len($finds) > 0
$Report = $finds
?? "Client Files Found: "
$wl = WriteLine( 3 , "Archive Log File Created: " + @TIME + " " + @DATE + @CRLF )
$wl = WriteLine( 3 , "Client Files Found: " + @CRLF )
GOSUB "Report"

if len($nofinds) > 0
$Report = $nofinds
?? "The following client file number did not match: "
$wl = WriteLine( 3 , @CRLF + @CRLF + "The following client file number did not match: " + @CRLF )
GOSUB "Report"
?? "Counted $count client file numbers in archlist.txt "
$wl = WriteLine( 3 , @CRLF + "Counted $count lines in archlist.txt " + @CRLF + @CRLF + @CRLF )
GOSUB "End"
else
GOSUB "End"
endif
else
GOSUB "End"
endif

ELSE
BEEP
? "failed to open file, error code : [" + @ERROR + "]"
ENDIF

:Report
$string = $Report
$delim = ","
$repcount = 0
; ? $string
$spot = instr($string,$delim)
; ? "What does Spot = " $spot ?
if $spot = 1

$string1 = substr("$string",1,$spot-1)
? $string1
$wl = WriteLine( 3 , $string1 + @CRLF )

While $spot <> 0
$repcount = $repcount + 1
$string = substr("$string",$spot+1,len("$string")-$spot)
$spot2 = instr($string,$delim)
$string2 = substr("$string",1,$spot2-1)
;? "Spot = " $spot ?
;? "Spot2 = " $spot2 ?
$spot = instr($string,$delim)
? $string2

If $string2 <> 0
$wl = WriteLine( 3 , $string2 + @CRLF )
endif
Loop
$string ?
$wl = WriteLine( 3 , $string + @CRLF )
$wl = WriteLine( 3 , "File Count: " + $repcount + @CRLF )
; ?$repcount?
else
$string
$wl = WriteLine( 3 , $string + @CRLF )
GOSUB "End"
endif
Return

:End




Top
#77363 - 2003-11-12 02:29 AM Re: Array
Howard Bullock Offline
KiX Supporter
*****

Registered: 2000-09-15
Posts: 5809
Loc: Harrisburg, PA USA
Substr may have a limitation to the size of the string it can process.

What is 'Gosub "End"'? That does not seem like very good programming style.

[ 12. November 2003, 02:31: Message edited by: Howard Bullock ]
_________________________
Home page: http://www.kixhelp.com/hb/

Top
#77364 - 2003-11-12 02:33 AM Re: Array
Les Offline
KiX Master
*****

Registered: 2001-06-11
Posts: 12734
Loc: fortfrances.on.ca
I'm not saying this is your problem, but why do you use quotes on this line?

$string1 = substr("$string",1,$spot-1)
_________________________
Give a man a fish and he will be back for more. Slap him with a fish and he will go away forever.

Top
#77365 - 2003-11-12 06:13 AM Re: Array
NTDOC Administrator Offline
Administrator
*****

Registered: 2000-07-28
Posts: 11628
Loc: CA
Because he is not using something like this

$iRC=SetOption('NoVarsInStrings','ON')

Otherwise it would really fail. [Wink]

Top
#77366 - 2003-11-13 08:00 AM Re: Array
ChristopheM Offline
Hey THIS is FUN
*****

Registered: 2002-05-13
Posts: 311
Loc: STRASBOURG, France
try the code below.
it read the file in an array that grows dynamically.
at the end, the array is truncated to the right size.

Function ReadFileToArray($filename)
Dim $handle
$handle=freefilehandle()
if open($handle,$filename)<>0
exit @error
endif

;-- prepare the array with an initial size --
Dim $incr, $size, $line, $index
$incr = 128
$size = $incr
dim $arr[$size]

;-- read file and put each line in the array --
$index = -1
$line = ReadLine($handle)
while not @error
$index = $index + 1
if $index>UBound($arr)
$size = $size + $incr
redim preserve $arr[$size]
endif
$arr[$index] = $line

$line = ReadLine($handle)
loop
;-- truncate the array to the minimum size --
redim preserve $arr[$index]

;-- close the file --
$_=close($handle)

;-- set the return data --
$ReadFileToArray=$arr
exit 0
EndFunction
_________________________
Christophe

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
1 registered (Allen) and 1198 anonymous users online.
Newest Members
M_Moore, BeeEm, min_seow, Audio, Hoschi
17883 Registered Users

Generated in 0.063 seconds in which 0.031 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