1

I'm trying to create a txt file for the reg query result which looks like this and works

REG QUERY HKLM\SOFTWARE\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall\{C5E637C6-5AB6-426F-B638-7DC533AE5C75} /v InstallLocation > C:\file.txt

But I'm trying to only create the txt file if the reg query finds something.

@echo off
REG QUERY HKLM\SOFTWARE\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall\{C5E637C6-5AB6-426F-B638-7DC533AE5C75} /v InstallLocation 
IF ERRORLEVEL 0 > C:\file.txt

I know I'm doing it wrong. with the above, it does create the .txt but it's blank. I want it to post the result so in this case InstallLocation

Sam
  • 45
  • 1
  • 1
  • 5

2 Answers2

0
@echo off
Set "Key=HKLM\SOFTWARE\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall\{C5E637C6-5AB6-426F-B638-7DC533AE5C75}"
Set "Val=InstallLocation"
Set "%Val%="
For /F "Tokens=1-2*" %%A in (
  'REG QUERY "%Key%" /v %Val% ^|Findstr /i "%Val%"'
) Do Set %Val%=%%C
IF Defind %Val% >C:\file.txt Call Echo:%%%Val%%% 

Alternatively replace last line with

SetLocal EnableExtensions EnableDelayedExpansion
IF Defind %Val% >C:\file.txt Echo:!%Val%!
0

At the command prompt:

For /F "Tokens=1,2*" %A In ('Reg Query "HKLM\SOFTWARE\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall\{C5E637C6-5AB6-426F-B638-7DC533AE5C75}" /V InstallLocation 2^>Nul') Do @If "%A"=="InstallLocation" >C:\file.txt Echo(%C

In a batch file:

@For /F "Tokens=1,2*" %%A In ('Reg Query^
 "HKLM\SOFTWARE\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall\{C5E637C6-5AB6-426F-B638-7DC533AE5C75}"^
 /V InstallLocation 2^>Nul') Do @If "%%A"=="InstallLocation" >C:\file.txt Echo(%%C

Just bear in mind that usually normal users do not have write permissions to C:\

Compo
  • 36,585
  • 5
  • 27
  • 39
  • This worked great! Thank you. I do have a question. If I was to use the same batch file to detected to different versions, can I use the same script and just change the {C5E637C6-5AB6-426F-B638-7DC533AE5C75} or do I have to change the Token names? – Sam Dec 19 '16 at 15:26
  • Sorry, for the delay in answering, I did not see that you had commented. Yes, there should be no reason why in most circumstances you cannot just change the name or GUID as you have indicated. – Compo Dec 19 '16 at 20:55