when I have
SET IrfanFile32=i_view32.exe
FOR /F "tokens=*" %%a IN ('dir /b /s "%ProgramFiles%\%IrfanFile32%" 2^>NUL') DO SET IrfanPath=%%a
this results in the wanted variable being set, if a corresponding file was found. But when I change the last line to
FOR /F "tokens=*" %%a IN ('dir /b /s "%PROGRAMFILES(X86)%\%IrfanFile32%"') DO SET IrfanPath=%%a
it says "\IrfanView\i_view32.exe" can't be processed syntactically at this point, which obviously refers to the set command. (echo works as gives the correct result). SET has a problem with the parenthesis it seems, but when I thought I could fix this by using quotes
FOR /F "tokens=*" %%a IN ('dir /b /s "%PROGRAMFILES(X86)%\%IrfanFile32%"') DO SET "IrfanPath=%%a"
I noticed that it doesn't fix it. What did I miss/how can I achieve this? (I read about delayedexpansion for normal variables but is that applicable for loop variables like %%a? if so: how?)
The following is the code as a whole:
REM @echo off
SETLOCAL ENABLEDELAYEDEXPANSION
SET IrfanFile32=i_view32.exe
SET IrfanFile64=i_view64.exe
SET IrfanParams=""
SET SourcePath=%~1\nul
:Start
FOR /F "tokens=3,4,5,6,7,8,9" %%a IN ('REG QUERY HKCU\Environment /v IrfanPath ^|find "IrfanPath"') DO (
REM echo %IrfanPath%
SET IrfanPath=%%a %%b %%c %%d %%e %%f %%g
)
echo.
IF NOT DEFINED IrfanPath (GOTO Init) ELSE (GOTO Main)
:Init
echo Dies scheint mein erster Start auf diesem System zu sein.
echo Ich benötige IrfanView (inkl. JPG_TRANSFORM Plugin) um zu funktionieren
echo und suche nun im Ordner %ProgramFiles% und allen Unterverzeichnissen
echo nach IrfanViews Startdatei namens %IrfanFile32% bzw. %IrfanFile64%.
echo.
IF EXIST "%PROGRAMFILES(X86)%" echo 64bit Betriebssystem gefunden, suche zusätzlich in %PROGRAMFILES(X86)%.
echo.
FOR /F "tokens=*" %%a IN ('dir /b /s "%ProgramFiles%\%IrfanFile32%" 2^>NUL') DO SET IrfanPath=%%a
FOR /F "tokens=*" %%a IN ('dir /b /s "%ProgramFiles%\%IrfanFile64%" 2^>NUL') DO SET IrfanPath=%%a
FOR /F "tokens=*" %%a IN ('dir /b /s "%PROGRAMFILES(X86)%\%IrfanFile32%"') DO SET IrfanPath=%%a
IF DEFINED IrfanPath (
echo Ich habe IrfanView hier gefunden: %IrfanPath%
:Entscheidung
SET /P PathKorrekt=Ist das der gewünschte Pfad? [^(J^)a/^(N^)ein]
if /i {!PathKorrekt!}=={j} (goto :yes)
if /i {!PathKorrekt!}=={ja} (goto :yes)
if /i {!PathKorrekt!}=={n} (goto :no)
if /i {!PathKorrekt!}=={nein} (goto :no)
echo Das war keine gültige Antwort. Benutze ja, nein, j oder n.
GOTO Entscheidung
:yes
REG ADD HKCU\Environment /v IrfanPath /d "%IrfanPath%" >nul
GOTO Start
)
:no
SET /P IrfanPath=Bitte gib den Pfad zur %IrfanFile32% bzw. %IrfanFile64% an:
IF NOT EXIST "%IrfanPath%\%IrfanFile32%" (
IF NOT EXIST "%IrfanPath%\%IrfanFile64%" (
echo Keine %IrfanFile32% oder %IrfanFile64% in diesem Ordner gefunden, nochmal bitte.
GOTO no
)
) ELSE (
IF EXIST "%IrfanPath%\%IrfanFile32%" (
REG ADD HKCU\Environment /v IrfanPath /d "%IrfanPath%\%IrfanFile32%"
)
IF EXIST "%IrfanPath%\%IrfanFile64%" (
REG ADD HKCU\Environment /v IrfanPath /d "%IrfanPath%\%IrfanFile64%"
)
PAUSE > NUL
GOTO Start
)
) else GOTO Main
:Main
REM more code here