Windows can't compare?

Hi, after my last question about why Windows accepts variables incorrectly, I now have a much stranger problem as shown in the attachment.

When I try to compare a variable with a string from a batch script, Windows says it's false. However, if I do it directly from the shell, I don't have any problems. What could be causing this?

(1 votes)
Loading...

Similar Posts

Subscribe
Notify of
6 Answers
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Tilo2300
2 years ago

Can you set your code copyable to a code day here?

In addition, at first glance, I would not use reserved commands for variable names (FOR).

In terms of the overview and in order to avoid errors due to spaces, I would also make more use of quotation marks.

Tilo2300
2 years ago
Reply to  thewindev

Your mistake is logical. The ELSE refers only to the last IF.

If getFileExt.bat has determined a .ISO, the FOR variable is initially set to ISO…

In the last IF (test to .esd), however, the condition is not fulfilled and the error message is output accordingly.

You would have to interlock the IF/ELSE several times or, after each of the first IF tests, insert a GOTO to your own jump mark immediately after the SET.

Tilo2300
2 years ago
@echo off
cls.
echo.

rem call "%workDir%\src\getFileExt\getFileExt.bat"
rem Angenommen, "getFileExt.bat" hat als "fileExtension" den Wert ".iso" angenommen
set "fileExtension=.iso"
rem --------------------------------------------------------


if /I %fileExtension% EQU .iso (set for=ISO)
rem   "%fileextension%" ist gleich ".iso", also wird "%for%" auf "ISO" gesetzt


if /I %fileExtension% EQU .wim (set for=WIM)
rem   "%fileextension%" ist immer noch ".iso", also ändert sich an "%for%" nichts (immer noch "ISO")


if /I %fileExtension% EQU .esd (set for=ESD) else (echo This is not an image file!! & echo. & goto imgSel01)
rem   "%fileextension%" ist immer noch ".iso", also ändert sich an "%for%" nichts (immer noch "ISO")
rem   Da "%fileextension%" aber NICHT ".esd" ist, wird der ELSE-Zweig abgearbeitet, also die Fehlermeldung ausgegeben.


:imgSel01
echo Sprungmarke "imgSel01" erreicht.


:ENDE01
echo Sprungmarke "ENDE01" erreicht.
echo fileextension: '%fileextension%'
echo for: '%for%'


pause
echo.
echo.


rem ========================================================================================
rem besser so:
rem ========================================================================================


set "for=dummy-value"


if /I %fileExtension% EQU .iso (set for=ISO && goto imgSel02)
rem   "%fileextension%" ist gleich ".iso", also wird "%for%" auf "ISO" gesetzt


if /I %fileExtension% EQU .wim (set for=WIM && goto imgSel02)
rem   "%fileextension%" ist immer noch ".iso", also ändert sich an "%for%" nichts (immer noch "ISO")


if /I %fileExtension% EQU .esd (set for=ESD && goto imgSel02) 
rem   "%fileextension%" ist immer noch ".iso", also ändert sich an "%for%" nichts (immer noch "ISO")


rem Wenn alle bisherigen IF-Abfragen keinen Treffer erzeielt haben und somit nicht zu einem GOTO-Sprung geführt haben...
echo This is not an image file!! & echo. & goto imgSel02


:imgSel02
echo Sprungmarke "imgSel02" erreicht.


:ENDE02
echo Sprungmarke "ENDE02" erreicht.
echo fileextension: '%fileextension%'
echo for: '%for%'


pause

Erzesel
2 years ago

devastating from a lot of forgotten “Quotes”…

the else branch only works the last if. This also affects the previously compared extensions by the else.

Remedial, if detected jump away, then at the end only the undesired remains…

set "file=blub.iso"

  rem wegen einer solch kleinen Routine  eine extra-Batch zu  bemühen  wäre mit Kanonen auf Spatzen  schießen
for %%i in ("%file%") do (set "fileExtension=%%~xi" & set fName=%%~ni)
  rem for ist ein Dämlicher VariablenName  ein Kommando  sollte man   nicht als variablenname  verwenden!!!! 
if /I "%fileExtension%" EQU ".iso" (set "Var=ISO" &goto :erkannt)
if /I "%fileExtension%" EQU ".wim" (set "Var=WIM" &goto :erkannt)
if /I "%fileExtension%" EQU ".esd" (set "Var=ESD" &goto :erkannt) 
echo This is not an image file!! 
echo:
goto imgSel

:erkannt
echo erkannt: %var%
pause
exit /b

:imgSel
echo blubb laber
pause




webfleet
10 months ago

have I not been saying?