#77360 - 2003-11-12 01:10 AM
Array
|
PhoeniX
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
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.
|
|
Top
|
|
|
|
#77362 - 2003-11-12 02:25 AM
Re: Array
|
PhoeniX
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
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 ]
|
|
Top
|
|
|
|
#77365 - 2003-11-12 06:13 AM
Re: Array
|
NTDOC
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.
|
|
Top
|
|
|
|
Moderator: Glenn Barnas, NTDOC, Arend_, Jochen, Radimus, Allen, ShaneEP, Ruud van Velsen, Mart
|
1 registered
(Allen)
and 1198 anonymous users online.
|
|
|