Based upon the code you have submitted, I'm not even sure why you would want to try to split the string at that particular place. There is a consistent string in every single line that would be returned by your reg.exe
command, and that is REG_
. The beauty of that paricular string is that it will always be non space separated, non localized, and never contain special characters. If you split at that point, you know that the substring you're looking for, will always be every token following its remainder, e.g. EXPAND_SZ your string(s)
; SZ your string(s)
.
So here's some example code which uses that method, but please be aware that it wil not work as intended should you have any variable defined within the System or User Environments with names including the case insensitive string REG_
:
@Echo Off
SetLocal EnableExtensions DisableDelayedExpansion
:Ask4Var
ClS
Set "sName="
Set /P "sName=Please enter the name of the variable you wish to verify>"
If Not Defined sName GoTo Ask4Var
(
Set %sName%
) 2>NUL | %SystemRoot%\System32\findstr.exe /BIL "%sName%=" 1>NUL || (
Echo There is no variable named %sName% in the current environment
%SystemRoot%\System32\timeout.exe /T 3 1>NUL
GoTo Ask4Var
)
Set "Env=System"
Set "RootKey=HKLM"
Set "SubKey=System\CurrentControlSet\Control\Session Manager\Environment"
%SystemRoot%\System32\choice.exe /C SU /N /M "[S]ystem OR [U]ser?"
If ErrorLevel 2 (
Set "Env=User"
Set "RootKey=HKCU"
Set "SubKey=Environment"
)
Set "Reg=%SystemRoot%\System32\reg.exe"
Set "ValueString="
For /F Delims^=^ EOL^= %%G In (
'%Reg% Query "%RootKey%\%SubKey%" /V /F "%sName%" /E ^|
%SystemRoot%\System32\find.exe "REG_"'
) Do (
Set "Result=%%G"
SetLocal EnableDelayedExpansion
For /F "Tokens=1,*" %%H In ("!Result:*REG_=!") Do (
EndLocal
Set "ValueString=%%I"
)
)
If Not Defined ValueString (
Echo There is no variable named %sName% in the %Env% environment
%SystemRoot%\System32\timeout.exe /T 3 1>NUL
GoTo Ask4Var
)
Echo The expanded string value of %sName% is %ValueString%.
%SystemRoot%\System32\timeout.exe /T 7 1>NUL
GoTo :EOF