Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How Can I List Programs in Alphabetical Order?

Status
Not open for further replies.

pbanacos

IS-IT--Management
Apr 21, 2006
34
US
Hi Guys,

I want to take a list of results generated by the code below and list it in alphabetical Order. Should I use a bubble sorting algorithm or does WMI have any native method or function to get this done?

Here is the snippet of code:


Const HKLM = &H80000002 'HKEY_LOCAL_MACHINE
strComputer = "."
strKey = "SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\"
strEntry1a = "DisplayName"

Set objReg = GetObject("winmgmts://" & strComputer & _
"/root/default:StdRegProv")
objReg.EnumKey HKLM, strKey, arrSubkeys
WScript.Echo "Installed Applications" & VbCrLf
For Each strSubkey In arrSubkeys
intRet1 = objReg.GetStringValue(HKLM, strKey & strSubkey, _
strEntry1a, strValue1)
If intRet1 <> 0 Then
objReg.GetStringValue HKLM, strKey & strSubkey, _
strEntry1b, strValue1
End If
If strValue1 <> "" Then
WScript.Echo strValue1
End If
Next "



Any help would be greatly appreciated.

Paul
 
I'm sure there is a way to do it with WMI as it is essentially an SQL query. But I wrote an arraySort to alphabetise a one dimensional array.

Code:
function arraySort(arr, intMode)
	'intMode = 0, Binary compare
	'intMode != 0, Textual compare
	for i = 0 to ubound(arr)
		str1 = arr(i)
		strTemp = str1
		intIndex = i
		for j = i to ubound(arr)
			intComp = strComp(str1, arr(j), intMode)
			if (intComp = 1) then
				str1 = arr(j)
				intIndex = j
			end if
		next
		arr(i) = arr(intIndex)
		arr(intIndex) = strTemp
	next
	arraySort = arr
end function

-Geates

"I hope I can chill and see the change - stop the bleed inside and feel again. Cut the chain of lies you've been feeding my veins; I've got nothing to say to you!"
-Infected Mushroom

"I do not offer answers, only considerations."
- Geates's Disclaimer
 
Another common methid is to use a disconnected recordset.
Something like (additions in blue)

Code:
[blue]
Const adVarChar = 200 
Const MaxCharacters = 255 
[/blue]
Const HKLM = &H80000002 'HKEY_LOCAL_MACHINE 
strComputer = "." 
strKey = "SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\" 
strEntry1a = "DisplayName"  

[blue]
Set DataList = CreateObject("ADODB.Recordset") 
 ' Add field title 
DataList.Fields.Append "ListItem", adVarChar, MaxCharacters 
' Open dataset 
DataList.Open 
[/blue]
Set objReg = GetObject("winmgmts://" & strComputer & _ 
 "/root/default:StdRegProv") 
objReg.EnumKey HKLM, strKey, arrSubkeys 
WScript.Echo "Installed Applications" & VbCrLf 

For Each strSubkey In arrSubkeys 
  intRet1 = objReg.GetStringValue(HKLM, strKey & strSubkey, _ 
   strEntry1a, strValue1) 
  If intRet1 <> 0 Then 
    objReg.GetStringValue HKLM, strKey & strSubkey, _ 
     strEntry1b, strValue1 
  End If 
  If strValue1 <> "" Then 
    WScript.Echo strValue1
[blue]
    DataList.AddNew  
    DataList("ListItem") = strValue1 
    DataList.Update  
[/blue]
  End If 
Next "  

[blue]
' Sort dataset 
DataList.Sort = "ListItem"  
' Go to beginning of list 
DataList.MoveFirst  
 
' Loop through dataset 
Do Until DataList.EOF 
    WScript.Echo DataList.Fields.Item("ListItem") 
    DataList.MoveNext 
Loop 
[/blue]


In order to understand recursion, you must first understand recursion.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top