1

I'm always getting the message "Echo is on" when trying to run the script..

setlocal ENABLEEXTENSIONS
C:
cd ..
cd ..
reg query "HKLM\SYSTEM\CurrentControlSet\Services" | findstr /b "HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\DummyService" > "C:\servicePaths.txt"
FOR /f "delims=" %%g IN (servicePaths.txt) do (
  set KEY_NAME="%%g"
  set VALUE_NAME=ImagePath
    FOR /F "tokens=1-3" %%A IN ('REG QUERY %KEY_NAME% /v %VALUE_NAME% 2^>nul') DO (
    set ValueValue=%%C
    )
  echo %ValueValue%
  pause
)

Could any batch-guru please help me? I know that it has something to do with the cascaded for-loops, but I cannot find the solutions - no clue anymore after 3 hours try&error.

1 Answer 1

2

It's a problem of expanding the %ValueValue%, as this happens when the complete parenthesis block is parsed.
Not when it is executed, but at parse time the ValueValue variable is empty, so you get only echo which will print echo is on.

Simply change it to delayed expansion

@echo off
setlocal EnableDelayedExpansion
C:
cd ..
cd ..
set VALUE_NAME=ImagePath

reg query "HKLM\SYSTEM\CurrentControlSet\Services" | findstr /b "HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\DummyService" > "C:\servicePaths.txt"
FOR /f "delims=" %%g IN (servicePaths.txt) do (
  set "KEY_NAME=%%g"
    FOR /F "tokens=1-3" %%A IN ('REG QUERY !KEY_NAME! /v !VALUE_NAME! 2^>nul') DO (
    set ValueValue=%%C
    )
  echo !ValueValue!
  pause
)
Sign up to request clarification or add additional context in comments.

1 Comment

You probably want to add @ECHO OFF at the top of the script also.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.