Page 1 of 1 1
Topic Options
#28032 - 2002-08-28 10:52 PM How Do I Sort the contents of a file alphabetically?
NTDOC Administrator Offline
Administrator
*****

Registered: 2000-07-28
Posts: 11634
Loc: Space
I have a text file that I've filled by enumerating the Registry. However, this text file is now not in any specific sorted order.

I would like to sort the contents into an alphabetical sorting and re-save the file.

Does anyone know how I can do this with KiXtart v4.1x ?

i.e.

Currently is:
Adobe Acrobat 5.0 5.0
Adobe Premiere 6.0 6.0
Advanced Port Scanner v1.1
Microsoft Windows BackOffice Resource Kit 2nd Edition: Legacy
Cleaner 5 EZ
ExamDiff Pro 2.8
MSN Messenger 4.6
HyperSnap-DX 4 4
InCtrl5
LANguard Network Scanner 1.00.0000
Intel(R) Processor Frequency ID Utility
LiveAdvisor (Symantec Corporation) 1.0.0.777
LiveUpdate 1.7 (Symantec Corporation)
Netscape Navigator 4.08
Ninotech Date Edit 4.0
Ninotech Path Copy 4.0
Atelier Web Security Port Scanner (v. 4.60)
Windows 2000 Hotfix (Pre-SP3) [See Q252795 for more information]
Windows 2000 Hotfix (Pre-SP3) [See Q276471 for more information]
Windows 2000 Hotfix (Pre-SP3) [See Q282784 for more information]

Should be:
Adobe Acrobat 5.0 5.0
Adobe Premiere 6.0 6.0
Advanced Port Scanner v1.1
Atelier Web Security Port Scanner (v. 4.60)
Cleaner 5 EZ
ExamDiff Pro 2.8
HyperSnap-DX 4 4
InCtrl5
Intel(R) Processor Frequency ID Utility
LANguard Network Scanner 1.00.0000
LiveAdvisor (Symantec Corporation) 1.0.0.777
LiveUpdate 1.7 (Symantec Corporation)
Microsoft Windows BackOffice Resource Kit 2nd Edition: Legacy
MSN Messenger 4.6
Netscape Navigator 4.08
Ninotech Date Edit 4.0
Ninotech Path Copy 4.0
Windows 2000 Hotfix (Pre-SP3) [See Q252795 for more information]
Windows 2000 Hotfix (Pre-SP3) [See Q276471 for more information]
Windows 2000 Hotfix (Pre-SP3) [See Q282784 for more information]

Top
#28033 - 2002-08-28 11:26 PM Re: How Do I Sort the contents of a file alphabetically?
MightyR1 Offline
MM club member
*****

Registered: 1999-09-09
Posts: 1264
Loc: The Netherlands
NTDOC,

take a look at: KiXtart Golf Tournament IV: Anagrams (Part 2, The code!)

It has a sort function:
code:
Function S($)
Dim $t
For $S = 1 TO Ubound($)
$t = $[$S-1]
If $[$S] < $t
$[$S-1] = $[$S]
$[$S] = $t
$S = 0
EndIf
Next
$S = $
EndFunction

So simply create an array of strings and use the sort function....
_________________________
Greetz,
Patrick Rutten

- We'll either find a way or make one...
- Knowledge is power; knowing how to find it is more powerful...
- Problems don't exist; they are challenges...

Top
#28034 - 2002-08-28 11:29 PM Re: How Do I Sort the contents of a file alphabetically?
Sealeopard Offline
KiX Master
*****

Registered: 2001-04-25
Posts: 11165
Loc: Boston, MA, USA
Or go into the UDF library for the sorting algorithms:
CombSort - The combsort algorithm for kixtart arrays
QSort() - quick sorting for (large) one dimensional arrays
BSort - Bubble sort for one dimensional arrays

Read the file with READLINE into an array. Sort the array, write into a file. Done.


Edited by NTDOC (2004-09-03 04:05 AM)
_________________________
There are two types of vessels, submarines and targets.

Top
#28035 - 2002-08-28 11:54 PM Re: How Do I Sort the contents of a file alphabetically?
Lonkero Administrator Offline
KiX Master Guru
*****

Registered: 2001-06-05
Posts: 22346
Loc: OK
did you gather the info from many places in reg?

as, normally reg values and keys are automatically sorted...

 
_________________________
!

download KiXnet

Top
#28036 - 2002-08-29 12:10 AM Re: How Do I Sort the contents of a file alphabetically?
NTDOC Administrator Offline
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 Offline
Korg Regular
*****

Registered: 2000-06-19
Posts: 714
Loc: Randers, Denmark
Doc, Here is an example using CombSort() (Fastest text sort algorithm until now) [Wink]

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
#28038 - 2002-08-29 03:39 AM Re: How Do I Sort the contents of a file alphabetically?
Sealeopard Offline
KiX Master
*****

Registered: 2001-04-25
Posts: 11165
Loc: Boston, MA, USA
I think KHolm pretty much nailed it down. [Smile]
_________________________
There are two types of vessels, submarines and targets.

Top
#28039 - 2002-08-29 04:22 AM Re: How Do I Sort the contents of a file alphabetically?
NTDOC Administrator Offline
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 Offline
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 Offline
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
_________________________
Utilize these resources:
UDFs (Full List)
KiXtart FAQ & How to's

Top
#28042 - 2002-08-29 07:16 AM Re: How Do I Sort the contents of a file alphabetically?
Kdyer Offline
KiX Supporter
*****

Registered: 2001-01-03
Posts: 6241
Loc: Tigard, OR
Doc,

This Rocks!

Kent
_________________________
Utilize these resources:
UDFs (Full List)
KiXtart FAQ & How to's

Top
#28043 - 2002-08-29 09:48 AM Re: How Do I Sort the contents of a file alphabetically?
NTDOC Administrator Offline
Administrator
*****

Registered: 2000-07-28
Posts: 11634
Loc: Space
Actually Vig, the sort command works different on Windows 9x then it does on NT/2000

This code works the same on all Windows OS and does not need to shell. Shelling takes time as well. This script ran in less then ONE second on my system that has 195 software installs on it.

Give it a try and compare it against the external SORT and I think this one will be faster.

Top
#28044 - 2002-08-29 09:59 AM Re: How Do I Sort the contents of a file alphabetically?
Lonkero Administrator Offline
KiX Master Guru
*****

Registered: 2001-06-05
Posts: 22346
Loc: OK
doc, this is almost short!
and still excellent...

like it... have to see how to steal it to my inventories...
_________________________
!

download KiXnet

Top
#28045 - 2002-08-29 04:40 PM Re: How Do I Sort the contents of a file alphabetically?
Fernando Madruga Offline
Starting to like KiXtart

Registered: 2002-08-21
Posts: 149
Loc: Coimbra.Portugal.Europe.Earth....
If you like the inventory sorted, the best thing would be to sort it before saving it to the file... [Smile]

(I know this is not what this thread is about: it's about sorting a file into another, but just thought of pointing that out for someone who might just copy/paste this code into their system)

Later,
Madruga
_________________________
Later,   [b]Mad[/b]ruga

Top
#28046 - 2002-08-29 05:26 PM Re: How Do I Sort the contents of a file alphabetically?
Lonkero Administrator Offline
KiX Master Guru
*****

Registered: 2001-06-05
Posts: 22346
Loc: OK
fernando, I wonder what part of the topic you read as doc's code sorts it before writing to file... [Roll Eyes]

 
_________________________
!

download KiXnet

Top
#28047 - 2002-08-29 05:55 PM Re: How Do I Sort the contents of a file alphabetically?
Sealeopard Offline
KiX Master
*****

Registered: 2001-04-25
Posts: 11165
Loc: Boston, MA, USA
Lonkero: The original post asked for sorting of a text file after it has been populated. further down, the moethod has bee refined to populate a software inventory into an arrya, then sort, then write into a file.

So, the focus has changed throughout the thread.
_________________________
There are two types of vessels, submarines and targets.

Top
#28048 - 2002-08-29 07:03 PM Re: How Do I Sort the contents of a file alphabetically?
NTDOC Administrator Offline
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
#28049 - 2002-08-29 07:41 PM Re: How Do I Sort the contents of a file alphabetically?
Sealeopard Offline
KiX Master
*****

Registered: 2001-04-25
Posts: 11165
Loc: Boston, MA, USA
Definitely a database approach because of the SQL querying.

I'm currently exporting all printer events into a SQL database and have a webpage display the printer volume by printer and user for each month of the year. The printer event export is done with a KiXtart script.

Reason: We can then bill each user [Wink]
_________________________
There are two types of vessels, submarines and targets.

Top
#28050 - 2002-08-29 08:22 PM Re: How Do I Sort the contents of a file alphabetically?
NTDOC Administrator Offline
Administrator
*****

Registered: 2000-07-28
Posts: 11634
Loc: Space
Good work Jens,

You're more advanced in that area then I. Using SQL for these tasks is kind of overkill for me. But in the long run porting this to a website would be cool. Just beyond my current free time to work it all out.

Hopefully some day I'll get around to doing that kind of reporting.

[ 29. August 2002, 20:40: Message edited by: NTDOC ]

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 2630 anonymous users online.
Newest Members
fgarcia, Viginette, ManuvdWielNL, Sir_Barrington, batdk82
17889 Registered Users

Generated in 0.28 seconds in which 0.137 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