0

I'm doing a simple script to check MS Office version reading registry key value. Will be scenarios where the key won't exist (not office installed) I used many times reg.exe query KEY/Check 2>nul but can't make that work inside a FOR line. This is the line:

FOR /f "tokens=3" %%a in ('%SystemRoot%\System32\reg.exe query "HKEY_CLASSES_ROOT\Word.Application\CurVer" ^| %SystemRoot%\System32\findstr /C:"(Default)"') do set _Off_ver=%%a

I tried:

FOR /f "tokens=3" %%a in ('%SystemRoot%\System32\reg.exe query "HKEY_CLASSES_ROOT\Word.Application\CurVer" 2>nul ^| %SystemRoot%\System32\findstr /C:"(Default)"') do set _Off_ver=%%a

or

FOR /f "tokens=3" %%a in ('%SystemRoot%\System32\reg.exe query "HKEY_CLASSES_ROOT\Word.Application\CurVer" ^| %SystemRoot%\System32\findstr /C:"(Default)"'2>nul ) do set _Off_ver=%%a

and in many other places but I end up with the error:

> was unexpected at this time.

Anyone know how I can hide the error:

ERROR: The system was unable to find the specified registry key or value.

Inside that FOR Line?

THANKS

DefToneR
  • 115
  • 8
  • 2
    `2^>nul` ........ – jeb Mar 16 '20 at 15:39
  • @jeb thanks! I just found the same, Still not sure why they use of ^ inside the FOR, but will read about it :) – DefToneR Mar 16 '20 at 15:44
  • I have no idea from your question whether you are trying to determine the version from a Click-to-Run or MSI installed Office product. If it's a pre-2019 MSI version, please feel free to take a look at [a previous answer of mine](https://stackoverflow.com/a/59700544) which, can do so, _(in that case, using the Word product)_. If you have any questions specific to that answer, feel free to comment appropriately under that answer. – Compo Mar 16 '20 at 16:02
  • Unable to hide, but also unable to catch... – Sandburg Mar 21 '22 at 16:16

1 Answers1

0

At the same time as Jeb replied I found a post (not related to this actually) that used 2^>nul. I'm not sure why the ^, but it worked.

The final line is:

FOR /f "tokens=3" %%a in ('%SystemRoot%\System32\reg.exe query "HKEY_CLASSES_ROOT\Word.Application\CurVer" 2^> nul ^| %SystemRoot%\System32\findstr /C:"(Default)"') do set _Off_ver=%%a

THANKS!

DefToneR
  • 115
  • 8
  • 1
    The reason is: The command inside the FOR is parsed by the batch parser, when there is an unescaped character like `>` or `|` it would be used on the `FOR` command itself, not on your `reg.exe` command, and that always fails. – jeb Mar 16 '20 at 16:15
  • same as for `^|`, which you did without questioning. – Stephan Mar 16 '20 at 17:38
  • Thanks for the information, I really appreciate it! :) – DefToneR Mar 19 '20 at 13:11