JSI Tip 6400. How can a batch file generate a .REG file that contains a REG_EXPAND_SZ data type with variable data?

Jerold Schulman

March 3, 2003

4 Min Read
ITPro Today logo in a gray background | ITPro Today


You may occasionally script a batch file that generates a .REG file. When the .REG file contains a REG_EXPAND_SZ data type with constant data, as in tip 5475, you can simply export a model and use it in your script. The 2 lines in tip 5475 that begin with @echo @=hex(2):25,53,79,73,74, is just such an example.

I have scripted RegXSZ.bat for the case when a model export is notconvenient or will not suffice.

The syntax for using RegXSZ.bat is:

Call RegXSZ ["]ValueName["] ["]string["] RegFile

where:

ValueName is the Value Name. If an unnamed Value Name is required, us either "", "@", or @.

string is the textual content, the data value. When specifying the string, the ! ^ & = | " ; , characters should NOT be used as they cause problems with the command interpreter. The % should also NOT be used, except to define special characters. The following table shows the special characters and the 3 character substitute that should be used instead.

Example: To specify a data value that contains "%ProgramFiles%Support ToolsKill.exe", "%22%25ProgramFiles%25Support ToolsKill.exe%22" is just one of the string arguments that could be used. So could %22%25ProgramFiles%25Support%20ToolsKill.exe%22 because I substituted %20 for the space character, eliminating the requirement to encapsulate the String in quote (") marks.

The following table contains a standard US English keyboard and the equivalent 3 character code for each key.

~

  %7e  

!

  %21  

@

  %40  

#

  %23  

$

  %24  

%

  %25  

^

  %5e  

&

  %26  

*

  %2a  

(

  %28  

)

  %29  

_

  %5f  

+

  %2b  

`

  %60  

1

  %31  

2

  %32  

3

  %33  

4

  %34  

5

  %35  

6

  %36  

7

  %37  

8

  %38  

9

  %39  

0

  %30  

-

  %2d  

=

  %3d  

Q

  %51  

W

  %57  

E

  %45  

R

  %52  

T

  %54  

Y

  %59  

U

  %55  

I

  %49  

O

  %4f  

P

  %50  

{

  %7b  

}

  %7d  

|

  %7c  

q

  %71  

w

  %77  

e

  %65  

r

  %72  

t

  %74  

y

  %79  

u

  %75  

i

  %69  

o

  %6f  

p

  %70  

[

  %5b  

]

  %5d  

  %5c  

A

  %41  

S

  %53  

D

  %44  

F

  %46  

G

  %47  

H

  %48  

J

  %4a  

K

  %4b  

L

  %4c  

:

  %3a  

"

  %22  

a

  %61  

s

  %73  

d

  %64  

f

  %66  

g

  %67  

h

  %68  

j

  %6a  

k

  %6b  

l

  %6c  

;

  %3b  

'

  %27  

Z

  %5a  

X

  %58  

C

  %43  

V

  %56  

B

  %42  

N

  %4e  

M

  %4d  

  %3c  

>

  %3e  

?

  %3f  

z

  %7a  

x

  %78  

c

  %63  

v

  %76  

b

  %62  

n

  %6e  

m

  %6d  

,

  %2c  

.

  %2e  

/

  %2f  

Space

%20  

NOTE: Any %nn combination can be used to account for other keyboards, special characters, or hexadecimal codes.

RegFile is the path to the output .REG file. Since there are alwaysdata lines before a Value Name, the file must already exist.

The RegXSZ.bat script returns a $RegXSZ environment variable that is set to Y when the REG_EXPAND_SZ line(s) in the .REG file is successfully created, or N when an error is encountered.

RegXSZ.bat contains:

@echo offset $RegXSZ=Nsetlocal DISABLEDELAYEDEXPANSIONif {%3}
{} goto syntaxset vn=%1set string=%2set RegFile=%3set fnd=Nif not exist %RegFile% goto syntaxset chars= ~@#$*()_+`1234567890-=QWERTYUIOP{}qwertyuiop[]ASDFGHJKL:asdfghjkl;'ZXCVBNM?zxcvbnm,./set charx1=20,7e,40,23,24,2a,28,29,5f,2b,60,31,32,33,34,35,36,37,38,39,30,2d,3d,51,57,45,52,54,59,55,49,4f,50,7b,7d,71,77,65,72,74,79,75,69,6f,set charx2=70,5b,5d,5c,41,53,44,46,47,48,4a,4b,4c,3a,61,73,64,66,67,68,6a,6b,6c,3b,27,5a,58,43,56,42,4e,4d,3f,7a,78,63,76,62,6e,6d,2c,2e,2f,ff,set charx=%charx1%%charx2%set vn=%vn:"=%if {%vn%}
{} set vn=@&goto l0if "%vn%"
"@" set vn=@&goto l0set vn="%vn%":l0set string=%string:"=%set work=%string:!=%If not "%work%" EQU "%string%" goto Syntaxset string=%string: =!20%set output=%vn%=hex(2):set /a outcnt=7:l1set /a outcnt=%outcnt% + 1call set work=%%output:~%outcnt%,1%%if {^%work%} EQU {^"} goto l1if not "%work%" EQU ":" goto l1set /a outcnt=%outcnt% + 1set /a incnt= -1:inlset /a incnt=%incnt% + 1call set work=%%string:~%incnt%,1%%if {%work%}
{} goto finishif "%work%" EQU "%%" goto spclif "%work%" EQU "!" goto spclsset fnd=Nset /a charcnt= -1:findloopset /a charcnt=%charcnt% + 1If %charcnt% EQU 87 goto finishcall set char=%%chars:~%charcnt%,1%%if not {^%work%} EQU {^%char%} goto findloopset /a charcnt=%charcnt% * 3call set spcw=%%charx:~%charcnt%,2%%set fnd=Y:outif "%fnd%" EQU "N" goto finishif %outcnt% lss 77 goto out1@echo %output%>>%regfile%set output= #set output=%output:#=%set /a outcnt=1:out1set output=%output%%spcw%,set /a outcnt=%outcnt% + 3goto inl:spclsset /a incnt=%incnt% + 1call set work=%%string:~%incnt%,2%%set /a incnt=%incnt% + 1set spcw=20if "%work%" EQU "%spcw%" set fnd=Y&goto outgoto syntax:spclset /a incnt=%incnt% + 1call set work=%%string:~%incnt%,2%%set /a incnt=%incnt% + 1set spcw=%work%set fnd=Ygoto out:syntax@echo Syntax: Call RegXSZ ["]ValueName["] ["]string["] RegFile (where RegFile must exist).set $RegXSZ=Nendlocalgoto :EOF:finishif "%fnd%" EQU "N" endlocal&set $RegXSZ=N&goto :EOFif %outcnt% EQU 1 goto fin1if %outcnt% lss 78 goto fin1@echo %output%>>%regfile%:fin1set output=%output%00@echo %output%>>%regfile%endlocal&set $RegXSZ=Y



Sign up for the ITPro Today newsletter
Stay on top of the IT universe with commentary, news analysis, how-to's, and tips delivered to your inbox daily.

You May Also Like