Registry adding tool (code)

Archives 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$

; 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


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"

; tells B+ where I want to read files from

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

; grab the next filename into file$

; 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


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

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
valid2% = ReadByte( filein ) ; other wise just load the value

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

z$=ReadLine (a) ; give the process some time
If z$=ok$ ; have we completed ok?
Write " done"+Chr$(10)
Until z$=ok$ Or Eof (a) ; end when the process has finished

; not the most sophisticated error handling, but it works.
Write " "+p\filename+" malformed."+Chr$(10)
tot=0 :valid1=0:valid2=0 ; reset all these values

CloseDir myDir

Print Chr$(10)+"kthxbye"



Print "Registry Runner v1.0 By em22"
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")

Function ego()
For n=0 To 90
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

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

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

reg import %exec% > NUL

GOTO :eof