#28036 - 2002-08-29 12:10 AM
Re: How Do I Sort the contents of a file alphabetically?
|
NTDOC
Administrator
   
Registered: 2000-07-28
Posts: 11634
Loc: Space
|
Same place Lonkero, and yes, they are sorted (by date of install - which is not what I want).
Now to let my ignorance of Array and these UDFs show.
Jens...
Do you mean something like this?
code:
Break On $tempfile=("c:\temp\@wksta.log") $tempfile2=("c:\temp\Inventory_@wksta.log") if open(1,$tempfile) = 0 $Array = readline(1) WHILE @error = 0 $DoSort=QSort($Array) $Array = readline(1) $NewFile=Open(2,$tempfile2,5) $wl=writeline(2,"$Array") Loop $x=Close(1) $x=Close(2) Endif
Which does not seem to work. Can you post an example please?
I'm working on modifying some code that BoxKite recently posted, but I wanted a sorted output.
|
|
Top
|
|
|
|
#28037 - 2002-08-29 01:46 AM
Re: How Do I Sort the contents of a file alphabetically?
|
kholm
Korg Regular
   
Registered: 2000-06-19
Posts: 714
Loc: Randers, Denmark
|
Doc, Here is an example using CombSort() (Fastest text sort algorithm until now)
code:
Break On Dim $aStr $tempfile=("c:\temp\@WkSta.log") If Open(1,$tempfile) = 0 $Line = ReadLine(1) While @error = 0 If $aStr $aStr = $aStr + '|' EndIf $aStr = $aStr + $Line $Line = ReadLine(1) Loop $x=Close(1) Else Return EndIf $UnSortedArray = Split($aStr,'|') $SortedArray = CombSort($UnSortedArray) $tempfile2=("c:\temp\Inventory_@WkSta.log") If Open(1,$tempfile2,5) = 0 For Each $Entry In $SortedArray $x = WriteLine(1,$Entry + @CRLF) Next $x=Close(1) EndIf Return ;************************************************************* ;Just for the ease i have included Shawn's CombSort() UDF ;************************************************************* Function CombSort($v, Optional $o) Dim $i,$j,$m,$s,$g $n=UBound($v) $g = $n If $g While $g > 1 Or Not $s $g=($g*1000)/1279 If $g < 1 $g = 1 EndIf $s=1 For $i = 0 To $n-$g $j=$i+$g If ($v[$i] > $v[$j] And Not $o) Or ($v[$i] < $v[$j] And $o) $m = $v[$i] $v[$i] = $v[$j] $v[$j] = $m $s=0 EndIf Next Loop $CombSort = $v Else $CombSort = 0 EndIf EndFunction
For explanation of CombSort() look here: The combsort algorithm for kixtart arrays [ 29. August 2002, 02:25: Message edited by: kholm ]
|
|
Top
|
|
|
|
#28039 - 2002-08-29 04:22 AM
Re: How Do I Sort the contents of a file alphabetically?
|
NTDOC
Administrator
   
Registered: 2000-07-28
Posts: 11634
Loc: Space
|
Well, after stealing some ideas and code from BoxKite and working with Bryce (Bryce did all the work) Here is a FAST method of doing an inventory on the installed software for a system.
You don't need the running time $aStart or $aStop, but I added to test the speed. On my system which has 195 entries, it completes the task in .78 seconds. Using other methods of writing a file first and then sorting it takes about 3 seconds. Using the old Csort method takes about 20 seconds.
code:
; GetInstalledSoftware.kix ; Idea borrowed/stolen from BoxKite ; Modifications made by NTDOC ; Very cool array and final coding by Bryce ; The Function CombSort is by Shawn break on $aStart=@ticks $outfile = "c:\temp\@wksta.log" $software = combsort(getinstalledsoftware)
if exist($outfile) del $outfile endif
$ = open(1, $outfile,5)
for each $item in $software $ = writeline(1,$item+@crlf) next
$ = close(1) $aStop=@ticks ? "Script Running Time is: " $aStop-$aStart exit(0)
function GetInstalledsoftware dim $softwarekey, $index dim $temparray[10]
$SoftwareKey = enumkey("HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\",$Index)
while not @error $DisplayName = readvalue("HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\$SoftwareKey","displayname") $DisplayVersion = readvalue("HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\$SoftwareKey","DisplayVersion") if $DisplayName if $count > ubound($temparray) redim preserve $temparray[ubound($temparray)+10] endif $temparray[$count] = $DisplayName +" "+ $DisplayVersion $count = $count + 1 endif $Index = $Index + 1 $SoftwareKey = enumkey("HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\",$Index) loop if $count > 0 redim preserve $temparray[$count-1] else $temparray = "" endif $getinstalledsoftware = $temparray endfunction
Function CombSort($v, Optional $o) Dim $i,$j,$m,$s,$g $n=UBound($v) $g = $n If $g While $g > 1 Or Not $s $g=($g*1000)/1279 If $g < 1 $g = 1 EndIf $s=1 For $i = 0 To $n-$g $j=$i+$g If ($v[$i] > $v[$j] And Not $o) Or ($v[$i] < $v[$j] And $o) $m = $v[$i] $v[$i] = $v[$j] $v[$j] = $m $s=0 EndIf Next Loop $CombSort = $v Else $CombSort = 0 EndIf EndFunction
[ 29. August 2002, 09:49: Message edited by: NTDOC ]
|
|
Top
|
|
|
|
#28040 - 2002-08-29 05:45 AM
Re: How Do I Sort the contents of a file alphabetically?
|
Vig
Starting to like KiXtart
Registered: 2001-11-14
Posts: 166
Loc: Saudi Arabia
|
Any reason you can't (or don't want to) use the sort cmd?
code:
shell '%comspec% /c type sort.txt | sort > sorted.txt'
|
|
Top
|
|
|
|
#28041 - 2002-08-29 07:01 AM
Re: How Do I Sort the contents of a file alphabetically?
|
Kdyer
KiX Supporter
   
Registered: 2001-01-03
Posts: 6241
Loc: Tigard, OR
|
Vig,
That would be nice.. However, I think Doc has a "pure" Kixtart solution.
Kent
|
|
Top
|
|
|
|
#28042 - 2002-08-29 07:16 AM
Re: How Do I Sort the contents of a file alphabetically?
|
Kdyer
KiX Supporter
   
Registered: 2001-01-03
Posts: 6241
Loc: Tigard, OR
|
|
|
Top
|
|
|
|
#28048 - 2002-08-29 07:03 PM
Re: How Do I Sort the contents of a file alphabetically?
|
NTDOC
Administrator
   
Registered: 2000-07-28
Posts: 11634
Loc: Space
|
Sorry there Jens and fernando,
I originally did not have the file read into an Array, so I asked how to sort a populated file.
This code in the thread should be able to do both, depending on how or what you want to do.
I worked with Bryce on this. I thought, well if we can do this other stuff with an Array, then why not just fill the array in memory, then write the file (cut out one step). Bryce came up with the code to put the Registry read into an array. Then you run it against the CombSort by Sean.
During all my testing it has been the fastest method so far.
I'm kind of with VIG on this one though. I have close to 3,000 machines to sort/store this data and then put in a reportable to management form.
OK I have the Software inventory - Now how do I report on it?
|
|
Top
|
|
|
|
Moderator: Jochen, Allen, Radimus, Glenn Barnas, ShaneEP, Ruud van Velsen, Arend_, Mart
|
0 registered
and 2175 anonymous users online.
|
|
|