Skip navigation

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


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 not convenient 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.

  !     %21     %     %25     ^     %5e     &     %26     =     %3d     |     %7c     "     %22     ;     %3b         %3c     >     %3e     ,     %2c  

Example: To specify a data value that contains "%ProgramFiles%\Support Tools\Kill.exe", "%22%25ProgramFiles%25\Support Tools\Kill.exe%22" is just one of the string arguments that could be used. So could %22%25ProgramFiles%25\Support%20Tools\Kill.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 always data 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 off
set $RegXSZ=N
setlocal DISABLEDELAYEDEXPANSION
if \{%3\}

\{\} goto syntax set vn=%1 set string=%2 set RegFile=%3 set fnd=N if not exist %RegFile% goto syntax set 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 l0 if "%vn%"

"@" set vn=@&goto l0 set vn="%vn%" :l0 set string=%string:"=% set work=%string:!=% If not "%work%" EQU "%string%" goto Syntax set string=%string: =!20% set output=%vn%=hex(2): set /a outcnt=7 :l1 set /a outcnt=%outcnt% + 1 call set work=%%output:~%outcnt%,1%% if \{^%work%\} EQU \{^"\} goto l1 if not "%work%" EQU ":" goto l1 set /a outcnt=%outcnt% + 1 set /a incnt= -1 :inl set /a incnt=%incnt% + 1 call set work=%%string:~%incnt%,1%% if \{%work%\}

\{\} goto finish if "%work%" EQU "%%" goto spcl if "%work%" EQU "!" goto spcls set fnd=N set /a charcnt= -1 :findloop set /a charcnt=%charcnt% + 1 If %charcnt% EQU 87 goto finish call set char=%%chars:~%charcnt%,1%% if not \{^%work%\} EQU \{^%char%\} goto findloop set /a charcnt=%charcnt% * 3 call set spcw=%%charx:~%charcnt%,2%% set fnd=Y :out if "%fnd%" EQU "N" goto finish if %outcnt% lss 77 goto out1 @echo %output%\>>%regfile% set output= # set output=%output:#=% set /a outcnt=1 :out1 set output=%output%%spcw%, set /a outcnt=%outcnt% + 3 goto inl :spcls set /a incnt=%incnt% + 1 call set work=%%string:~%incnt%,2%% set /a incnt=%incnt% + 1 set spcw=20 if "%work%" EQU "%spcw%" set fnd=Y&goto out goto syntax :spcl set /a incnt=%incnt% + 1 call set work=%%string:~%incnt%,2%% set /a incnt=%incnt% + 1 set spcw=%work% set fnd=Y goto out :syntax @echo Syntax: Call RegXSZ \["\]ValueName\["\] \["\]string\["\] RegFile (where RegFile must exist). set $RegXSZ=N endlocal goto :EOF :finish if "%fnd%" EQU "N" endlocal&set $RegXSZ=N&goto :EOF if %outcnt% EQU 1 goto fin1 if %outcnt% lss 78 goto fin1 @echo %output%\>>%regfile% :fin1 set output=%output%00 @echo %output%>>%regfile% endlocal&set $RegXSZ=Y



Hide comments

Comments

  • Allowed HTML tags: <em> <strong> <blockquote> <br> <p>

Plain text

  • No HTML tags allowed.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Lines and paragraphs break automatically.
Publish