Q:

What's wrong with the syntax of this batch script?

I'm trying to autoload Putty Pageant with some SSH-keys with a batch script, but because I want to bypass the error message of Pageant that it is already running, I placed it in an IF-statement. For some reason however, it doesn't work:
tasklist /FI "IMAGENAME eq pageant.exe" 2>NUL | find /I /N "pageant.exe">NUL
if %ERRORLEVEL%==1 ( :: checks whether pageant.exe is running or not

    :: set the SSH-keys
    if exist "C:\SSH keys\key1.ppk" (set KEYS="C:\SSH keys\key1.ppk")
    if exist "C:\SSH keys\key2.ppk" (set KEYS=%KEYS% "C:\SSH keys\key2.ppk")

    if not defined KEYS (
        msg * A SSH-key is propably missing.
    )

    :: Start pageant with the defined SSH-keys
    start /d"C:\Program Files (x86)\PuTTY" pageant.exe %KEYS%

)

While they do work separately:
(1)
 tasklist /FI "IMAGENAME eq pageant.exe" 2>NUL | find /I /N "pageant.exe">NUL
 if %ERRORLEVEL%==1 ( :: checks whether pageant.exe is running or not

       :: This works!
       start /d"C:\Program Files (x86)\PuTTY" pageant.exe

 )

(2)
:: set the SSH-keys
if exist "C:\SSH keys\key1.ppk" (set KEYS="C:\SSH keys\key1.ppk")
if exist "C:\SSH keys\key2.ppk" (set KEYS=%KEYS% "C:\SSH keys\key2.ppk")

if not defined KEYS (
    msg * A SSH-key is propably missing.
)

This works as well!
start /d"C:\Program Files (x86)\PuTTY" pageant.exe %KEYS%

Is it a syntax problem?

A:

Without an error message I can only guess.
Do you have delayed expansion enabled?  See setlocal /?.
Add this to the beggining of your script
setlocal EnableExtensions EnableDelayedExpansion
and
endlocal to the end of your script.
This allows the KEYS variable to evaluate the the actual value.  Delayed expansion allows the value to be set immediately to the variable not just when the scope of the if statement has ended. Also don't forget to use ! instead of % for the variables set inside the if statement.
Example: (Run this in a .bat once with the EnableDepayedExpansion and once without and you will see the difference.)
setlocal EnableExtensions EnableDelayedExpansion

set "Value=Hello World"
echo %Value%
if 1==1 (
    set "Value=Goodbye World"
    echo %Value%
    echo !Value!
)
echo %Value%

endlocal

