WinBatch Tech Support Home

Database Search

If you can't find the information using the categories below, post a question over in our WinBatch Tech Support Forum.

TechHome

Sample Code from Users

Can't find the information you are looking for here? Then leave a message over on our WinBatch Tech Support Forum.

Registry Scavenger


;*************************************************************************************
;*** Get all the subkeys and values of subkeys for a given key.
;***
;*************************************************************************************



; ###################################################################################
; ### Start GetSubKeys_UDF 

#DefineFunction GetSubKeys_UDF(subCount, subKeyNames, realKeyPath, delim, backslash)
  subKeyPaths=""

  For ZZ = 1 to subCount
     thisKey=ItemExtract(ZZ, subKeyNames, @TAB)
     subKeyPaths=StrCat(subKeyPaths, @TAB, delim, realKeyPath, backslash, thisKey)
  Next ZZ

  subKeyPaths=StrTrim(subKeyPaths)
  subKeyPaths=StrReplace(subKeyPaths,  @TAB, @CR)

  Return (subKeyPaths)

#EndFunction

; ### End GetSubKeys_UDF 
; ###################################################################################





; ###################################################################################
; ### Start RegKeyValues_UDF 

#DefineFunction RegKeyValues_UDF(base, HK, realKeyPath, delim, backslash)
  Value_List=""
  separator="==="
  delim="*"

  Key=RegOpenKey(HK, realKeyPath)
  Allitems = RegQueryItem(key, "")
  count=ItemCount(Allitems, @TAB)


  ; *** Return empty string if there are no values in this key
  If count==0 then Return ("")

  For XX = 1 to count
   item=ItemExtract(XX, Allitems, @TAB)
   type=RegEntryType(key, "[%item%]")
   clipput(type)


   errormode(@OFF)
   value=RegQueryEx(key, "[%item%]", delim, type)
   errormode(@CANCEL)

	
   ;*** type' must be defined as one of the following
   Switch type

     Case 0
      typeName="REG_NONE"
		break

     Case 1
      typeName="REG_SZ"
		break

     Case 2
      typeName="REG_EXPAND_SZ"
		break

     Case 3 
      typeName="REG_BINARY"
		break

     Case 4
      typeName="REG_DWORD"
		break

     Case 5
      typeName="REG_DWORD_BIG_ENDIAN"
		break

     Case 6
      typeName="REG_LINK"
		break

     Case 7
      typeName="REG_MULTI_SZ"
		break

     Case 8
      typeName="REG_RESOURCE_LIST"
		break

     Case 9
      typeName="REG_FULL_RESOURCE_DESCRIPTOR"
		break

     Case 10
      typeName="REG_RESOURCE_REQUIREMENTS_LIST"
		break

   EndSwitch ; type


   Value_List=StrCat(Value_List, @TAB, realKeyPath, backslash, item, separator, typeName, separator, value)
   clipput(value_list)

  Next XX

RegCloseKey(key)

Value_List=StrTrim(Value_List)
Value_List=ItemSort(Value_List, @TAB)
Value_List=StrReplace(Value_List, @TAB, @CR)

Return(Value_List)

#EndFunction

; ### End RegKeyValues_UDF 
; ###################################################################################






; *********************************
; *** Begin Main processing
; *********************************

just_one="TRUE"

;*** Set up a delimiter that (hopefully) will never be found in the registry.
delim=">^<"
delim_length=StrCharCount(delim)
backslash="\"

Reg=""
While Reg==""
RegDialogFormat=`WWWDLGED,6.1`

RegDialogCaption=`Registry Information`
RegDialogX=-01
RegDialogY=-02
RegDialogWidth=162
RegDialogHeight=130
RegDialogNumControls=007
RegDialogProcedure=`DEFAULT`
RegDialogFont=`DEFAULT`
RegDialogTextColor=`DEFAULT`
RegDialogBackground=`DEFAULT,DEFAULT`
RegDialogConfig=0

RegDialog001=`021,101,036,012,PUSHBUTTON,DEFAULT,"OK",1,1,32,DEFAULT,DEFAULT,DEFAULT`
RegDialog002=`101,101,036,012,PUSHBUTTON,DEFAULT,"Cancel",0,2,DEFAULT,DEFAULT,DEFAULT,DEFAULT`
RegDialog003=`015,005,128,012,STATICTEXT,DEFAULT,"Enter full path to the registry key",DEFAULT,3,512,"Microsoft Sans Serif|6656|70|34","0|0|0",DEFAULT`
RegDialog004=`015,033,128,012,EDITBOX,Reg,DEFAULT,DEFAULT,4,DEFAULT,DEFAULT,DEFAULT,DEFAULT`
RegDialog005=`029,055,102,014,RADIOBUTTON,RadVar,"Search only Local Machine",1,5,DEFAULT,"Microsoft Sans Serif|6656|40|34","0|0|0",DEFAULT`
RegDialog006=`029,075,102,014,RADIOBUTTON,RadVar,"Search Remote Computers",2,5,DEFAULT,"Microsoft Sans Serif|6656|40|34","0|0|0",DEFAULT`
RegDialog007=`025,019,110,010,STATICTEXT,DEFAULT,"i.e.   HKEY_LOCAL_MACHINE\SOFTWARE",DEFAULT,7,DEFAULT,DEFAULT,DEFAULT,DEFAULT`

ButtonPushed=Dialog("RegDialog")





EndWhile


;*** Parse out the HKEY info and the path.
slash=StrIndex(Reg, backslash, 1, @FWDSCAN)
base=StrSub(Reg, 1, slash-1)
extend=StrSub(Reg, slash+1, -1)

List=StrCat(delim, extend, @CR)
Orig_List=List

If base=="HKEY_CLASSES_ROOT"  then HK=@REGCLASSES
If base=="HKEY_CURRENT_USER"  then HK=@REGCURRENT
If base=="HKEY_LOCAL_MACHINE" then HK=@REGMACHINE
If base=="HKEY_USERS"         then HK=@REGUSERS
root=HK   ; for multiple machine searches.


;*** Detirmine if it's just the local machine to scan...
If radVar==1 then GoSub Run_Search


;*** ...otherwise, scan multiple machines.
;*** Can only connect to HKEY_LOCAL_MACHINE or HKEY_USERS, though.
If base=="HKEY_CLASSES_ROOT" || base=="HKEY_CURRENT_USER"
   Message("Ooop, sorry!", "Can only connect to remote 'HKEY_LOCAL_MACHINE'%@CRLF%or 'HKEY_USERS'.  Terminating.")
   exit
EndIf

   nodes=""
   While nodes==""
      NodeDialogFormat=`WWWDLGED,6.1`

      NodeDialogCaption=`Remote Computers`
      NodeDialogX=-02
      NodeDialogY=-01
      NodeDialogWidth=118
      NodeDialogHeight=156
      NodeDialogNumControls=004
      NodeDialogProcedure=`DEFAULT`
      NodeDialogFont=`DEFAULT`
      NodeDialogTextColor=`DEFAULT`
      NodeDialogBackground=`DEFAULT,DEFAULT`
      NodeDialogConfig=0

      NodeDialog001=`011,135,036,012,PUSHBUTTON,DEFAULT,"OK",1,1,32,DEFAULT,DEFAULT,DEFAULT`
      NodeDialog002=`065,135,036,012,PUSHBUTTON,DEFAULT,"Cancel",0,2,DEFAULT,DEFAULT,DEFAULT,DEFAULT`
      NodeDialog003=`019,037,074,088,MULTILINEBOX,nodes,DEFAULT,DEFAULT,3,DEFAULT,DEFAULT,DEFAULT,DEFAULT`
      NodeDialog004=`021,007,072,026,STATICTEXT,DEFAULT,"Enter or copy in your list of remote machines to scan",DEFAULT,4,DEFAULT,"Microsoft Sans Serif|6656|70|34","0|0|0",DEFAULT`

      ButtonPushed=Dialog("NodeDialog")

   EndWhile

just_one="FALSE"

;*** Strip off any backslashes in case they were entered.
nodes=StrReplace(nodes, "\\", "")

;*** Need to replace CRLF with a single character delimiter.
nodes=StrReplace(nodes, @CRLF, @TAB)
nodes=StrTrim(nodes)

count=ItemCount(nodes, @TAB)

;*** Open file for writing results.
daFile=fileOpen("C:\Temp\results.txt", "WRITE")

For XX = 1 to Count
thisNode=ItemExtract(XX, nodes, @TAB)

HK=regconnect("\\%thisNode%", root)

gosub Run_Search

list=StrReplace(list, @CR, @CRLF)
FileWrite(daFile,"[%thisNode%]")
FileWrite(daFile,list)
FileWrite(daFile, @CR)
list=Orig_List

Next  XX

;*** Al done with multiple machine search.
FileCLose(daFile)
Message("Data saved in...", "C:\Temp\results.txt" )
Exit



:Run_Search
While @TRUE
   ;*** Check the list for more subkeys.
   ;*** Locate next delimiter
   startItem=StrIndex(List, delim, 0, @FWDSCAN)
   If startItem==0 && Just_one=="TRUE" then goto FINI   ; all done
   If startItem==0 && Just_one=="FALSE" then Return     ; process next node in the list

   endItem=StrIndex(List, @CR, startItem, @FWDSCAN)

   ;*** Pull out the reg key w/path, including the delimiter (>^<).
   tempPath=StrSub(List, startItem, endItem-StartItem)

   ;*** Remove the delimiter.
   realKeyPath=StrSub(tempPath, delim_length+1, -1)
   BoxOpen("Processing...", realKeyPath)
   ;*** Locate where this item is in the list and replace with actual path (delims removed).
   index=ItemLocate(tempPath, List, @CR)
   List=ItemReplace(realKeyPath, index, list, @CR)
   

   ;*** Look for any subkeys to the parent.
   Key=RegOpenKey(HK, realKeyPath)
   subKeyNames=RegQueryKeys(key)
   RegCloseKey(Key)
   subcount=ItemCount(subKeyNames, @TAB)

   ;*** If there are no subkeys, go check for any values under this key.
   ; If subcount==0 then GoTo FindValues  ; <----- un-remark if you want values returned.


   ;*** Process UDF to list any subkeys
   subKeyPaths=GetSubKeys_UDF(subCount, subKeyNames, realKeyPath, delim, backslash) 

   ;*** Plug the returned subkeys into the list immediately below the parent item.
   List=ItemInsert(subKeyPaths, index, list, @CR)


  :FINDVALUES
   ;*** Check for any values under this key.
  ; value_List=RegKeyValues_UDF(base, HK, realKeyPath, delim, backslash) ; <----- un-remark if you want values returned.

   ;*** Plug the returned values into the list immediately below the parent item.
   ; List=ItemInsert(value_List, index, list, @CR) ; <----- un-remark if you want values returned.


EndWhile


;*** Done with local machine processing
:FINI
BoxShut()
list=StrReplace(list, @CR, @CRLF)
FilePut("C:\Temp\RegOutput.txt", list)
Message("","All Done")
Run("C:\Temp\RegOutput.txt", "")
exit


Article ID:   W16671
File Created: 2005:02:18:12:21:50
Last Updated: 2005:02:18:12:21:50