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.

Create a WBT File Based on a REG File


A while ago someone asked whether there was a function to create a WBT file based on a .reg file. Well, here is my attempt at it. I am sure there is some bugs and that it was not the best way to go about it, but it appears to work. Let me know of any issues and be sure to test the script out, especially the RegMulSz keys.

;******************************************************************************
;          		Program Information
;******************************************************************************
; Reg2WBT.wbt
; Created by Brad Sjue 
; Date: September 02, 2003
; This script will This will create a ConvertedReg.wbt file based on a reg file

; To Do List/Known Issues
; Currently only search for 3 delimiters in the MulSz key.  
; May run into an issue with the number of StrCat's, apparently the limit is 72
; May run into issues with the line being greater than 255.  

; Revisions:
;   1.  Originally written as an INI read, but ran into the issue of 255.
;   2.  Used a fileread then, but ran into an issue that the line was too long.  
;   3.  Changed to a variable using a fileget
; 	4.  Had to break the regcreate key to separate lines because the line was too long. 

;******************************************************************************
;          		Initialize Program
;******************************************************************************

;Optional Debugging
If IsKeyDown(@SHIFT || @CTRL)    
	sTrcFile = Strcat(Environment("TEMP"),"\~Reg2Wbt.trc")
	If FileExist(sTrcFile) Then FileDelete(sTrcFile)
	DebugTrace(@ON,sTrcFile)
Endif

CurrPath = FilePath(IntControl (1004, 0, 0, 0, 0))

;******************************************************************************
;          		Subroutines and Functions
;******************************************************************************

#DefineFunction Hex2Dec(hex)
   str="0123456789ABCDEF"
   hex=StrTrim(StrUpper(hex))
   hexlen=StrLen(hex)
   dec=0
   for x=1 to hexlen
       dec=(dec*16) + StrIndex(str,strsub(hex,x,1),0,@fwdscan) -1
   next
   return(dec)
#EndFunction

;******************************************************************************
;          		Main Program
;******************************************************************************

types="Reg Files|*.reg|All Files|*.*|"
OrigRegFile = AskFileName("Registry File to convert to Winbatch", "C:\", types, "", 1)

WBTOut = StrCat(CurrPath, "ConvertedReg.wbt")

If !FileExist(OrigRegFile)
	Message("Registry File Error!", "Could not determine existence of registry file, exiting")
	Exit
Endif

;Check if WBTOut already exists
If Fileexist(WBTOut)
	q=Askyesno("Warning",strcat("File:",WBTOut,@CRLF,"Already exists, overwrite it?"))
	if q==@NO Then Exit
Endif

RegFile = FileGet(OrigRegFile)
RepStr = StrCat(",\", @CRLF, "  ")
RegFile = StrReplace(RegFile, RepStr, "~")	;Looking at the hex lines and converting them to one line.
RegFile = StrReplace(RegFile, @CRLF, @TAB)	;Put in a list format

ERRORMODE (@OFF)
fh = FileOpen(WBTOut, "Write")
if !fh
	Message("Error",strcat("File: ",fh,@CRLF,"Could not be opened"))
	Exit
endif
ERRORMODE (@CANCEL)

BoxOpen ('Registry to WBT Conversion', 'Processing')
HeadMsg = StrCat('; Reg2WBT - Auto-Generated Winbatch File', @CRLF)
FileWrite(fh, HeadMsg)	;Write Header

HexReg = StrCat('#DefineFunction ConvertHexReg(RegValue)', @CRLF)
HexReg = StrCat(HexReg, '	HexCnt = ItemCount (RegValue, ",")', @CRLF)
HexReg = StrCat(HexReg, '	MulSzValue = ""', @CRLF)
HexReg = StrCat(HexReg, '	For x = 1 to HexCnt', @CRLF)
HexReg = StrCat(HexReg, '		Hex1 = ItemExtract (x, RegValue, ",")', @CRLF)
HexReg = StrCat(HexReg, '		Dec = Hex2Dec(Hex1)', @CRLF)
HexReg = StrCat(HexReg, '		Char = Num2Char (Dec)', @CRLF)
HexReg = StrCat(HexReg, '		MulSzValue = ItemInsert (Char, -1, MulSzValue, @TAB)', @CRLF)
HexReg = StrCat(HexReg, '		MulSzValue = StrReplace(MulSzValue, @TAB, "")', @CRLF)
HexReg = StrCat(HexReg, '	Next', @CRLF)
HexReg = StrCat(HexReg, '	Return MulSzValue', @CRLF)
HexReg = StrCat(HexReg, '#EndFunction', @CRLF)
FileWrite(fh, HexReg)	;Write Function

HexDec = StrCat('#DefineFunction Hex2Dec(hex)', @CRLF)
HexDec = StrCat(HexDec, '   str="0123456789ABCDEF"', @CRLF)
HexDec = StrCat(HexDec, '   hex=StrTrim(StrUpper(hex))', @CRLF)
HexDec = StrCat(HexDec, '   hexlen=StrLen(hex)', @CRLF)
HexDec = StrCat(HexDec, '   dec=0', @CRLF)
HexDec = StrCat(HexDec, '   for x=1 to hexlen', @CRLF)
HexDec = StrCat(HexDec, '       dec=(dec*16) + StrIndex(str,strsub(hex,x,1),0,@fwdscan) -1', @CRLF)
HexDec = StrCat(HexDec, '   next', @CRLF)
HexDec = StrCat(HexDec, '   return(dec)', @CRLF)
HexDec = StrCat(HexDec, '#EndFunction', @CRLF)
FileWrite(fh, HexDec)	;Write Function

RegCnt = ItemCount( RegFile, @TAB)
For x = 1 to RegCnt
	line = ItemExtract( x, RegFile, @TAB)
	If line == "*EOF*" Then Break
	If line == "" Then Continue
	If line == "REGEDIT4" Then Continue
	If line == "Windows Registry Editor Version 5.00" Then Continue
	
	If StrIndexWild(line, '[*]', 1) != 0	;Test for Key Structure, if so write Key. 
		line = StrReplace(line, '[', '')	;Remove the bracket
		line = StrReplace(line, ']', '')	;Remove the bracket
	
		If StrIndexNc(line, "HKEY_CLASSES_ROOT", 1, @FWDSCAN) != 0 
		RegHandle = "@REGCLASSES"
		CreateKeyLoc = StrReplace(line, "HKEY_CLASSES_ROOT\", "")  ;Remove the Handle and the trailing backslash
		KeyCreate = StrCat('If !RegExistKey(', RegHandle, ', "', CreateKeyLoc, '") ',@CRLF,' RegCreateKey(', RegHandle, ', "', CreateKeyLoc, '")', @CRLF, 'Endif')
		FileWrite(fh, KeyCreate)	;Write Key Creation
		Continue
		Endif
		
		If StrIndexNc(line, "HKEY_LOCAL_MACHINE", 1, @FWDSCAN) != 0 
		RegHandle = "@REGMACHINE"
		CreateKeyLoc = StrReplace(line, "HKEY_LOCAL_MACHINE\", "")
		KeyCreate = StrCat('If !RegExistKey(', RegHandle, ', "', CreateKeyLoc, '") ',@CRLF,' RegCreateKey(', RegHandle, ', "', CreateKeyLoc, '")', @CRLF, 'Endif')
		FileWrite(fh, KeyCreate)	;Write Key Creation
		Continue
		Endif
		
		If StrIndexNc(line, "HKEY_CURRENT_USER", 1, @FWDSCAN) != 0 
		RegHandle = "@REGCURRENT"
		CreateKeyLoc = StrReplace(line, "HKEY_CURRENT_USER\", "")
		KeyCreate = StrCat('If !RegExistKey(', RegHandle, ', "', CreateKeyLoc, '") ',@CRLF,' RegCreateKey(', RegHandle, ', "', CreateKeyLoc, '")', @CRLF, 'Endif')
		FileWrite(fh, KeyCreate)	;Write Key Creation
		Continue
		Endif
		
		If StrIndexNc(line, "HKEY_USERS", 1, @FWDSCAN) != 0 
		RegHandle = "@REGUSERS"
		CreateKeyLoc = StrReplace(line, "HKEY_USERS\", "")
		KeyCreate = StrCat('If !RegExistKey(', RegHandle, ', "', CreateKeyLoc, '") ',@CRLF,' RegCreateKey(', RegHandle, ', "', CreateKeyLoc, '")', @CRLF, 'Endif')
		FileWrite(fh, KeyCreate)	;Write Key Creation
		Continue
		Endif

    Endif

; 		-----------------	REGBINARY  -----------------
	If StrIndexNc (line, "hex:", 1, @FWDSCAN) !=0	
		BinValue = ItemExtract (1, line, "=")	;Get the Bin Value
		BinValue = StrReplace(BinValue, '"', "")
		BinData = ItemExtract (2, line, "=")	;Get the Bin Data
	    If ItemCount (BinData, ",") < 1 Then Continue ; Could not find a comma in the value, test just incase the data has the word Hex in it. 
; 		Replace all of the ~'s with a StrCat and extract out each section
	 	BinData = Strreplace(BinData, "hex:", "")
	 	BinData = Strreplace(BinData, ",", " ")
		BinCnt = ItemCount (BinData, "~")
	 	FileWrite(fh, 'BinData = ""')
		For y = 1 to BinCnt
			NewBinData = ItemExtract (y, BinData, "~")
			If y == 1 
				NewBinData = StrCat('BinData = StrCat(BinData,"', NewBinData, '")')			
			Else
				NewBinData = StrCat('BinData = StrCat(BinData," ', NewBinData, '")')			
			Endif
			
		 	FileWrite(fh, NewBinData)
	 	Next
	 	WriteBin = StrCat('RegSetBin(', RegHandle, ', "' , CreateKeyLoc, '[', BinValue, ']", BinData)')
	 	FileWrite(fh, WriteBin)
	 	Continue
	Endif 

; 		-----------------	REG_EXPAND_SZ  -----------------
	If StrIndexNc (line, "hex(2)", 1, @FWDSCAN) !=0	
	ExpSzValue = ItemExtract (1, line, "=")	;Get the ExpSz Value
	ExpSzValue = StrReplace(ExpSzValue, '"', "")
	ExpSzData = ItemExtract (2, line, "=")	;Get the ExpSz Data
    If ItemCount (ExpSzData, ",") < 1 Then Continue ; Could not find a comma in the value, test just incase the data has the word Hex in it. 
 	ExpSzData = Strreplace(ExpSzData, "hex(2):", "")
	ExpSzCnt = ItemCount (ExpSzData, "~")
 	FileWrite(fh, 'ExpSzData = ""')
	For y = 1 to ExpSzCnt
		NewExpSzData = ItemExtract (y, ExpSzData, "~")
		If y == 1 Then
			NewExpSzData = StrCat('ExpSzData = StrCat(ExpSzData,"', NewExpSzData, '")')			
	 	Else
			NewExpSzData = StrCat('ExpSzData = StrCat(ExpSzData,",', NewExpSzData, '")')			
		Endif
		FileWrite(fh, NewExpSzData)
 	Next
 	FileWrite(fh, 'ExpSzData = ConvertHexReg(ExpSzData)')
 	WriteExpSz = StrCat('RegSetExpSz(', RegHandle, ', "' , CreateKeyLoc, '[', ExpSzValue, ']", ExpSzData)')
 	FileWrite(fh, WriteExpSz)
 	Continue
	Endif 
				
; 		-----------------	REG_MULTI_SZ  -----------------
	If StrIndexNc (line, "hex(7)", 1, @FWDSCAN) !=0	
	MulSzValue = ItemExtract (1, line, "=")	;Get the MulSz Value
	MulSzValue = StrReplace(MulSzValue, '"', "")
	MulSzData = ItemExtract (2, line, "=")	;Get the MulSz Data
    If ItemCount (MulSzData, ",") < 1 Then Continue ; Could not find a comma in the value, test just incase the data has the word Hex in it. 
 	MulSzData = Strreplace(MulSzData, "hex(7):", "")
	MulSzCnt = ItemCount (MulSzData, "~")
 	FileWrite(fh, 'MulSzData = ""')
	For y = 1 to MulSzCnt
		NewMulSzData = ItemExtract (y, MulSzData, "~")
		If y ==1 
			NewMulSzData = StrCat('MulSzData = StrCat(MulSzData,"', NewMulSzData, '")')			
		Else
			NewMulSzData = StrCat('MulSzData = StrCat(MulSzData,",', NewMulSzData, '")')			
		Endif
	 	FileWrite(fh, NewMulSzData)
 	Next
 	FileWrite(fh, 'MulSzData = ConvertHexReg(MulSzData)')
 	FileWrite(fh, 'If StrScan (MulSzData, "@TAB", 1, @FWDSCAN) != 0 Then RegDelim = @TAB')
	FileWrite(fh, 'If StrScan (MulSzData, ",", 1, @FWDSCAN) != 0 Then RegDelim = ","')
	FileWrite(fh, 'If StrScan (MulSzData, " ", 1, @FWDSCAN) != 0 Then RegDelim = " "')

 	WriteMulSz = StrCat('RegSetMulSz(', RegHandle, ', "' , CreateKeyLoc, '[', MulSzValue, ']",MulSzData, RegDelim)')
 	FileWrite(fh, WriteMulSz)
 	Continue
	Endif 
		
; 		-----------------	REG_DWORD  -----------------
	If StrIndexNc (line, "dword", 1, @FWDSCAN) !=0	
	DwordValue = ItemExtract (1, line, "=")	;Get the Dword Value
	DwordValue = StrReplace(DwordValue, '"', "")
	DwordData = ItemExtract (2, line, "=")	;Get the Dword Data
 	DwordData = Strreplace(DwordData, "dword:", "")
 	DwordData=Hex2Dec(DwordData)
 	WriteDword = StrCat('RegSetDword(', RegHandle, ', "' , CreateKeyLoc, '[', DwordValue, ']", ', DwordData,')')
 	FileWrite(fh, WriteDword)
 	Continue
	Endif 
	
; 		-----------------	STRING VALUE  -----------------
	If StrIndexWild(line, '"*"', 1) != 0	;Test for String Values within the Key.  
		StrValue = ItemExtract (1, line, "=")	;Get the String Value
		StrValue = StrReplace(StrValue, '"', "")
		StrData = ItemExtract (2, line, "=")	;Get the String Data
		StrData  = Strreplace(StrData, "\\", "\")	 ; Do a global replace on the \\ because the registry exports it out with a double slash
		WriteValue = StrCat('RegSetValue(', RegHandle, ', "' , CreateKeyLoc, '[', StrValue, ']", ', StrData,')')
		FileWrite(fh, WriteValue)	;Write RegValues
	Endif
    
Next

FileClose(fh)
BoxShut()
Message("Completed Registry Conversion", "Completed registry conversion")

;******************************************************************************
;          		End of Program
;******************************************************************************
Exit
;EOF

Article ID:   W16175
File Created: 2004:03:30:15:43:08
Last Updated: 2004:03:30:15:43:08