user3011014
user3011014

Reputation: 11

Issue With Path Expansion Batch Script

I am having a problem with Windows path expansion using a batch file where the variable contains a string from the Windows registry. See script below. I have tried multiple methods but I must be missing something simple.

@echo off
setlocal enabledelayedexpansion

for /f "tokens=2*" %%a in ('reg.exe query "HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services" /t REG_EXPAND_SZ /f ServiceDll /s ^| grep.exe -ia "REG_EXPAND_SZ" ') do (
set registry_value2=%%b
call :regmerge
)
goto :endofscript

:regmerge
echo !registry_value2!
goto :eof

:endofscript
endlocal

Upvotes: 0

Views: 57

Answers (2)

MC ND
MC ND

Reputation: 70923

Replace

set registry_value2=%%b

with

call set "registry_value2=%%b"

and remove the call to regmerge. The call will force the parser to do a second pass and replace the environment variables references.

Upvotes: 1

rene
rene

Reputation: 42444

Here is a possible solution with the use of findstr and storing the intermediate result in a temp file.

@echo off
rem tempfile
set tf=regfind.tmp

rem reg query output pipe into findstr and redirect to temnpfile
reg.exe query "HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services" /t REG_EXPAND_SZ /v ServiceDll /s | findstr /C:SystemRoot > %tf% 

rem iterate over each line in temp file and call merge
for /F "tokens=3*" %%a in (%tf%) do call :merge %%a

rem delete tempfile
del /q %tf%

goto :eof

:merge
rem SystemRoot is exapanded now...
echo %1 

goto :eof

Upvotes: 0

Related Questions