0

I wrote a batch file using reg query. There is a for loop in the batch file.
The following message is output on running the batch file:

命令语法不正确。

This is Chinese which means:

The syntax of the command is incorrect.

The batch file producing this error is:

@echo off

for /F "tokens=1,2 delims=:" %%a in ('reg query "HKEY_CURRENT_USER\Software\WinRAR SFX" /v "C%%Program Files (x86)%Cloud Tool Service"')
do 
(   
    set "value1=%%a"
    echo %value1%
    set "value2=%%b" 
    echo %value2%
)

set "install_dir=%value1:~-1%:%value2%"

Can anyone help me the reason for this syntax error?

Mofi
  • 46,139
  • 17
  • 80
  • 143
Leon
  • 31
  • 8
  • 2
    You need to put the `do` on the same line as the `for`. – Klitos Kyriacou Jul 24 '19 at 12:23
  • @KlitosKyriacou I put "do" on the same line as the "for". But it's still not working: for /F "tokens=1,2 delims=:" %%a in ('reg query "HKEY_CURRENT_USER\Software\WinRAR SFX" /v "C%%Program Files (x86)%Cloud Tool Service"') do ( set "value1=%%a" echo %value1% set "value2=%%b" echo %value2% ) – Leon Jul 24 '19 at 12:32
  • 1
    I think there might be a problem with percent-signs in `"C%%Program Files (x86)%Cloud Tool Service"`. Also, use delayed expansion inside the parentheses (Google it if you don't know what delayed expansion is). – Klitos Kyriacou Jul 24 '19 at 12:40
  • `) do (` must be on the same line, so also the perentheses. Any you need [delayed expansion](https://ss64.com/nt/delayedexpansion.html) when you write *and* read variables in the same block of code (so for echoing in the loop, but not for the last line)... – aschipfl Jul 24 '19 at 15:15

1 Answers1

1

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 /?
Mofi
  • 46,139
  • 17
  • 80
  • 143