Registry adding tool (code)

BlitzPlus Forums/BlitzPlus Programming/Registry adding tool (code)

em22(Posted 2009) [#1]
Here is an a little app I wrote so I could tailor unattended builds where I work. After writing this I found I could do the same in a batch script but easier with BlitzPlus to add more features like file validation. I've added as much comments as I could, it also shows a way to execute commands from within your B+ program. Any improvements would be nice.

; Simply console app that adds registry files. Can be easily customized.
; by em22

AppTitle "RegRunner 1.1"

; Set some variables
Global ok$,z$
done=0

; create a type for our file names
Type file
	Field filename$
End Type

; Lets see if any command lines have been passed. If not the display help

folder$=CommandLine$()

If folder$="" Or folder$="/?" Then Goto help
If folder$="-c" Then folder$=CurrentDir$()
If Right(folder$,1)<>"\" Then folder=folder+"\"

If FileType(folder$)=0 
	Notify "Error "+folder$+" not found exiting. Use /? For help"
	End
EndIf

; tells B+ where I want to read files from
myDir=ReadDir(folder$)

Print "Registry Runner v1.1 by em22 (c)2009"+Chr$(10)
Print "Please wait scanning : "+folder$+"..."

Repeat
	; grab the next filename into file$
	file$=NextFile(myDir)
	
	; If there isn't another one, let's exit this loop

	If file$="" Then Exit
	
	; we only want reg fils and not folders, this sorts that out.
	
	If FileType(folder$+"\"+file$) <> 2 And Right(file$,3)="reg" Then
		p.file = New File
		p\filename =file$
		count=count+1 ; increment a count we will use to know if we found any reg files.
	End If

Forever

If count=0 ; no reg files so...
	Print "No reg keys found to add, exiting."+Chr$(10)
	ego()
	End
EndIf 

Print "Found "+count+" registry file(s) to add :"+Chr$(10)
Print "Adding registry keys :"+Chr$(10)

;Stop ; debugging purposes
	
;we need to go through our list of reg files we found.
	
For p.File = Each File

	; first lets check that our reg files have been formed the correct way
	; so all I do is check the bytes from position 0 and 1 and add them up.
	; some files when created in Windows will start with 255 then 254 (FF,FE)
	; I handle this after doing the first calculation. I'm looking for "W" and then
	; an "i" from the "Windows Registry Editor Version 5.00" at the start of the reg
	; file.
	
	fin$=folder$+p\filename  ; sets fin$ and the next valid filename 
	filein=OpenFile(fin$)   ; open the file 
	SeekFile(filein,0)    ; seek to begining
	valid1%= ReadByte( filein )   ; read the value
	valid2%= ReadByte( filein )   ; and the next
	CloseFile(filein)   ; the close the file

	tot=valid1+valid2  ; add the two values
	
	If tot=509   
	; 509 is the total from Windows formatted reg files rather than raw ascii data
	; so I need To check then next 3 bytes, as there will now be a "00" between the 
	; values I want eg. "W",00,"i"
	
		filein=OpenFile(fin$) ; open the file again
		SeekFile(filein,2) ; start at 2 bytes in
		valid1%= ReadByte( filein ) ; read the next byte in value1
		If ReadByte( filein ) = 0 ; if the NEXT byte = 0  then
			valid2% = ReadByte( filein ) ; valid should equal the byte after
		Else 
			valid2% = ReadByte( filein ) ; other wise just load the value
		EndIf	 
		CloseFile(filein) 
		tot=valid1+valid2
	EndIf 

	If tot=192 ; it the ASCII value of "W" and "i" added together
	
		; set our string that we want to execute
		f$="reg import "+Chr$(34)+folder$+p\filename+Chr$(34) 
		Write " "+p\filename+"..."
		
		; set the successful return message when the registry has been updated.
		ok$="The operation completed successfully"
		
		a=CreateProcess(f$)  ; create a process
	
		Repeat  
			z$=ReadLine (a) ; give the process some time
			If z$=ok$ ; have we completed ok?
				done=1 
				Write " done"+Chr$(10) 
			EndIf			
		Until z$=ok$ Or Eof (a) ; end when the process has finished
	
	Else
		; not the most sophisticated error handling, but it works.
		Write " "+p\filename+" malformed."+Chr$(10)
	EndIf	
	tot=0 :valid1=0:valid2=0 ; reset all these values
Next

CloseDir myDir

Print Chr$(10)+"kthxbye"

ego()

End


.help
Print "Registry Runner v1.0 By em22 em22ukgmail.com"
Print "(C)2009 Free for personal use"+Chr$(10)
Print "RegRunner is a program for adding multiple registry entries that are contained 
Print "within a folder as separate .reg files. This can be useful For automated"
Print "builds giving a quick and easy way to add or take away settings in one go."
Print ""
Print "Use -c and it will look in the current directory for .reg files." 
Print "If you supply a pathname RegRunner will look there."+Chr$(10)
Print "To make a batch script wait for RegRunner use:"+Chr$(10)
Print "cmd.exe /c regrun.exe [-c] [path]"+Chr$(10)
Input$("Press Return")
End

Function ego()
	t=CreateTimer(60)
	For n=0 To 90
	WaitTimer(t)
	Next 
End Function



em22(Posted 2009) [#2]
And here is how to do it in a batch script. :)

@echo off
echo Regrun batch version, (C)2009 em22
echo.

SET count=0
set chd=%1
FOR /f "tokens=1,2 delims=." %%G IN ('dir /b /a-d %1') DO if "%%H"== "reg" (call :s_do_reg %%G %%H)
echo Added %count% reg entries.
goto :EOF

:s_do_reg
 set /a count+=1
 set exec=%chd%%1.%2
 echo Applying %exec%

 reg import %exec% > NUL

GOTO :eof