In order to get the current Office installation path, I set up this line
reg query "HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\App Paths\WINWORD.EXE"
and the result is:
(Default) REG_SZ C:\PROGRA~1\MICROS~1\Office16\WINWORD.EXE
Path REG_SZ C:\Program Files\Microsoft Office\Office16\
useURL REG_SZ 1
SaveURL REG_SZ 1
How to grep out the "C:\Program Files\Microsoft Office\Office16\" in a variable? Thanks.
By using the command line
reg query "HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\App Paths\winword.exe" /v Path
just the string value of Path
is output which means on Windows XP:
! REG.EXE VERSION 3.0
HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\App Paths\winword.exe
Path REG_SZ C:\Program Files\Microsoft Office\Office16\
So this output starts with an empty line, a header of reg.exe version 3.0, one more empty line, the queried registry key and the queried registry value Path
if found at all in Windows registry under specified key. There is a tab character between Path
and REG_SZ
and one more tab character between REG_SZ
and the path string. The line with Path
starts with four spaces.
On Windows Vista and later Windows versions the output is:
HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\App Paths\winword.exe
Path REG_SZ C:\Program Files\Microsoft Office\Office16\
This output starts with an empty line like on Windows XP. But output is next already the queried registry key without any additional header. Last the line with queried registry value Path
is output if found at all in Windows registry under specified key. The last line starts also with four spaces like on Windows XP. But there are four spaces between Path
and REG_SZ
and four spaces between REG_SZ
and the path string instead of horizontal tabs.
This means for getting the path string using command FOR with option /F
:
Path
or one more non empty line needs to be processed to have a batch file working also on Windows XP.Path
, REG_SZ
and the path string which could contain also 1 or more spaces or any other character allowed in a folder name.The batch file code for this task:
@echo off
for /F "skip=2 tokens=1,2*" %%A in ('%SystemRoot%\System32\reg.exe query "HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\App Paths\winword.exe" 2^>nul') do (
if /I "%%A" == "Path" if not "%%~C" == "" set "OfficePath=%%~C" & goto FoundPath
)
echo Could not determine MS Office path. MS Office is most likely not installed.
echo/
pause
goto :EOF
:FoundPath
rem Remove backslash at end of path if there is one at all.
if "%OfficePath:~-1%" == "\" set "OfficePath=%OfficePath:~0,-1%"
echo MS Office is installed in: "%OfficePath%"
rem Other command using environment variable OfficePath.
The command FOR executes in a background process started with cmd.exe /C
the command line (with %SystemRoot%
already expanded):
%SystemRoot%\System32\reg.exe query "HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\App Paths\winword.exe" 2>nul
The output of this command process (= reg.exe
) written to handle STDOUT is captured by FOR.
REG outputs to handle STDERR an error message if either the specified registry key or the specified registry value does not exist at all in Windows registry. This error output is redirected to device NUL to suppress it. Read the Microsoft article about Using Command Redirection Operators for an explanation of 2>nul
. The redirection operator >
must be escaped with caret character ^
on FOR command line to be interpreted as literal character when Windows command interpreter processes this command line before executing command FOR which executes the embedded reg
command line with using a separate command process started in background.
The command FOR could have nothing to process in case of registry key or registry value not found by REG resulting in execution of the command lines below the FOR loop informing the batch file user about this use case.
Otherwise command FOR skips because of skip=2
the first two captured lines which means on Windows Vista and later the first line processed by FOR is already the line containing Path
. On Windows XP the third line being an empty line is ignored by FOR and the next line with queried registry key is processed next.
The line is split up because of tokens=1,2*
and the default delimiters space/tab into 3 substrings.
On Windows Vista and later Windows:
Path
is assigned to specified loop variable A
.REG_SZ
is assigned to next loop variable B
according to ASCII table.C:\Program Files\Microsoft Office\Office16\
is assigned to loop variable C
.*
.On Windows XP the first line tokenized by FOR results in:
HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\App
being assigned to specified loop variable A
andPaths\winword.exe
being assigned to next loop variable B
.C
because there is no more string on processed line.A case-insensitive string comparison of value of loop variable A
is made with fixed string Path
to verify if FOR processed already the right line with Path
value. On Windows Vista and later Windows versions this is already true on first line tokenized by FOR. On Windows XP this condition is first false and FOR processes therefore the next line now assigned the same strings to the loop variables A
, B
and C
as on Windows Vista and later Windows versions.
On a true condition the path string assigned to loop variable C
and not being an empty string is assigned to environment variable OfficePath
with removing enclosing double quotes if the path string is enclosed at all in "
. And next a jump is made to label FoundPath
exiting the loop and continue batch file execution in code block on having the MS Office path.
In this code block first a backslash at end of path is removed if there is one at all to have assigned to environment variable OfficePath
always the path string without a backslash at end independent on path string in registry not having or having a backslash at end using string substitution.
For understanding the used commands and how they work, open a command prompt window, execute there the following commands, and read entirely all help pages displayed for each command very carefully.
echo /?
for /?
goto /?
if /?
pause /?
reg /?
reg query /?
rem /?
set /?