Open a command prompt, run for /?
and read the output help. do
must be on line with closing )
of the set and (
of the command block to execute must be on same line as keyword do
, otherwise Windows command processor exits batch file processing with error message:
The syntax of the command is incorrect.
Then run set /?
and read again the output help, especially the chapter about delayed expansion explained on an IF and a FOR example. echo %value1%
will always result in execution of just echo
printing current state of echo mode, except environment variable value1
is defined already by chance before execution of for
.
The Windows command processor parses the entire command block from (
to matching )
and replaces all %variable%
by current value of the referenced environment variable before executing command for
. This can be seen on debugging the batch file.
See also: How does the Windows Command Interpreter (CMD.EXE) parse scripts?
And last to query the registry string value with name C%%Program Files (x86)%Cloud Tool Service
as indeed stored exactly as written here by WinRAR in Windows registry hive of current user, it is necessary to escape each percent sign in batch file with one more percent sign.
A batch file code with error checks working even on Windows XP is:
@echo off
setlocal EnableExtensions DisableDelayedExpansion
set "InstallDirectory="
for /F "skip=2 eol=H tokens=1-6*" %%I in ('%SystemRoot%\System32\reg.exe query "HKEY_CURRENT_USER\Software\WinRAR SFX" /v "C%%%%Program Files (x86)%%Cloud Tool Service" 2^>nul') do (
if /I "%%I %%J %%K %%L %%M" == "C%%%%Program Files (x86)%%Cloud Tool Service" (
if "%%N" == "REG_SZ" (
set "InstallDirectory=%%O"
) else if "%%N" == "REG_EXPAND_SZ" (
call set "InstallDirectory=%%O"
)
if defined InstallDirectory goto CheckDirectory
)
)
echo Could not find installation directory of Cloud Tool Service in registry.
goto EndBatch
:CheckDirectory
rem Remove backslash at end of installation directory if there is one at all.
if "%InstallDirectory:~-1%" == "\" set "InstallDirectory=%InstallDirectory:~0,-1%"
if exist "%InstallDirectory%\" (
echo Cloud Tool Service installed in: "%InstallDirectory%"
) else (
echo Cloud Tool Service directory "%InstallDirectory%" does not exist.
)
:EndBatch
rem Restore initial environment which results in discarding the environment
rem variable InstallDirectory as defined by the code above on directory found
rem in Windows registry of current user account.
endlocal
To understand the commands used and how they work, open a command prompt window, execute there the following commands, and read the displayed help pages for each command, entirely and carefully.
call /?
echo /?
endlocal /?
for /?
goto /?
if /?
reg /?
and reg query /?
rem /?
set /?
setlocal /?