@ECHO OFF SETLOCAL set KEY_NAME="HKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Node\My Entry" set KEY_NAME="HKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Node\Space Sciences Laboratory, UC Berkeley\BOINC Setup" set VALUE_NAME=migrationdir FOR /F "usebackq skip=2 tokens=1,2*" %%A IN ( `REG QUERY %KEY_NAME% /v %VALUE_NAME% 2^>nul`) DO ( set "ValueName=%%A" set "ValueType=%%B" set Home="%%C" ) IF DEFINED home SET home=%home:"=% if defined Home echo Home = %Home% if NOT defined Home echo %KEY_NAME%\%VALUE_NAME% not found.
Since I do not have the key ...\My Entry as the key, I replaced the key that I have.
The first element - since the required data contains a space, and the space is the default separator, you need to use 1,2* as tokens - the first, second and remaining lines.
The next small difficulty is that the ALSO data element contains ) , which is interpreted as the closing bracket FOR/f , therefore, gives an error - in your case \Dir1\Dir2 not expected here
To overcome this, you need to quote, then divide the value assigned to home
But again, the closing bracket in home serves to complete the TRUE part of the IF statement.
the inclusion / absence of the value assigned by HOME overcomes this. Obviously, the same can be said for Valuename , although there is a hidden failure.
If the requested registry entry is not found, then SET in FOR/f not executed, so the values โโof the three variables do not change from any value that they can have before executing FOR/f . Therefore, if home (or Valuename , if you decide to use this value) has a value of someexistingvalue before FOR/f , then unexpected results may be presented.
(BTW - it is legitimate and tidier to break FOR/f in both or both brackets of the IN clause)