0

I need to query the registry for HKCU\Software\test If this file exists I need to write the computer name to a log file. c:\Log.txt. I can query the registry but I have not been able to figure out how to use the if statement to add the computer name to the log file. Any help would be appreciated.

reg query "hkcu\software\test"
echo %COMPUTERNAME% >> c:\Log.txt
2
  • Show us the relevant part of your code. Commented Nov 8, 2011 at 12:53
  • reg query "hkcu\software\test" echo %COMPUTERNAME% >> c:\Log.txt Commented Nov 17, 2011 at 17:44

3 Answers 3

1
echo %COMPUTERNAME% >> c:\Log.txt

thats all :)

Sign up to request clarification or add additional context in comments.

1 Comment

reg query "hkcu\software\test" echo %COMPUTERNAME% >> c:\Log.txt
1

Try this:

@ECHO OFF
REG QUERY "HKCU\Software\test" >nul 2>&1
IF %ERRORLEVEL%==0 ECHO %COMPUTERNAME%>>C:\Log.txt

The >nul 2>&1 will hide the output of the REG command. If you want to see the output, just remove that part.

Comments

0

You could use the same approach as in this answer to your previous question, only use && in this case:

REG QUERY "whatever\you\want\to\query" >NUL && ECHO %COMPUTERNAME%>>C:\Log.txt

Similarly to FINDSTR, REG also sets ERRORLEVEL to a non-zero value if the search was unsuccessful, which allows us to use constructs with || and && as appropriate. The command after && is executed only if the search has been successful.

The above command suppresses standard output of REG with >NUL. If the search fails, the corresponding error message will still be displayed, because it is sent to the standard error device, rather than to the standard output. You can additionally suppress possible error messages by adding 2>NUL or like in @aphoria's answer.

Comments

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.