How to compare a string in batch file? - batch-file

I am writing a batch script as follows:
#echo off
setlocal enabledelayedexpansion
set Current_Node="Node1"
if "%Current_Node%" == "Node1" (
echo "ITS INSIDE IF LOOP"
) else (
echo "ITS NOT INSIDE IF LOOP"
)
Now,according to above code it should not go to the else part but in actual it is always entering to the else part.
I am not getting whether the problem is in syntax or the way i am comparing the strings.
So, please help me out.

You have too many quotes. Don't use quotes when setting the value or do not use quotes on "%Current_Node%".
When you use Current_Node="Node1", %Current_Node% will be equal to "Node1", but "%Current_Node%" will be equal to ""Node1"".
#echo off
setlocal enabledelayedexpansion
set Current_Node=Node1
echo %Current_Node%
if "%Current_Node%" == "Node1" (
echo ITS INSIDE IF LOOP
) else (
echo ITS NOT INSIDE IF LOOP
)

As suggested, there are quotes used where not necessary. I believe you need to use EQU instead of == etc. Try below if still having trouble.
#echo off
setlocal enabledelayedexpansion
set Current_Node=Node1
if %Current_Node% EQU Node1 (
echo ITS INSIDE IF LOOP
) else (
echo ITS NOT INSIDE IF LOOP
)

Double quotes are sometimes needed when setting a variable, so the solution can be simply to wrap the term in double quotes as seen in line two below. They do not become part of the variable.
#echo off
set "Current_Node=Node1"
if "%Current_Node%" == "Node1" (
echo "ITS INSIDE IF LOOP"
) else (
echo "ITS NOT INSIDE IF LOOP"
)

Related

Strange behavior inside IF block in a batch file

I have this batch file here and it doesn't run well. It says "( was unexpected at this time". Do you know why?
#ECHO OFF
SET A[0]=HOLA
SET A[1]=HELLO
SET I=0
:LOOP
IF DEFINED A[%I%] (
ECHO %I%
CALL SET B=%%A[%I%]%%
CALL ECHO %%B%%
IF %B% == HOLA (
ECHO ES
) ELSE (
ECHO EN
)
PAUSE
SET /A I+=1
GOTO :LOOP )
My expected output should be:
0
HOLA
ES
1
HELLO
EN
I don't understand why inside IF everything works different. Thanks in advance.
#ECHO OFF
SETLOCAL
SET "A[0]=HOLA"
SET "A[1]=HELLO"
SET /a I=0
:LOOP
IF DEFINED A[%I%] (
ECHO %I%
CALL SET "B=%%A[%I%]%%"
CALL ECHO %%B%%
FOR /f "delims==" %%b IN ('set B') DO IF /i "%%b"=="B" (IF "%%c"=="HOLA" (SET "Same=Y") ELSE SET "Same=")
IF DEFINED Same (
ECHO ES
) ELSE (
ECHO EN
)
rem PAUSE
SET /A I+=1
GOTO LOOP
)
GOTO :EOF
Use set "var=value" for setting string values - this avoids problems caused by trailing spaces. Don't assign a terminal \, Space or " - build pathnames from the elements - counterintuitively, it is likely to make the process easier. If the syntax set var="value" is used, then the quotes become part of the value assigned.
Your set syntax sets the value of the variable to the rest of the line.
Hence, the line including the label loop would be assigned to i.
if %B%... substitutes the value of B at the time the block was parsed. AT that time, B was not defined, so nothing is substituted, so the result is if == HOLA (... and cmd does not expect ( at this point and objects.
The complex line I've shown performs a set command on all variables that start b and assigns %%b to the variable name, %%c to its current value. There may be many variables that start B, so we need to select exactly B, and if B's value in %%c is HOLA then set Same, otherwise set Same to nothing, which means that Same will either be assigned, or not assigned respectively.
if defined operates on the current value of the variable, so same can be used safely to do the if processing.
BUT The clean way to do this is by using delayedexpansion.
Stephan's DELAYEDEXPANSION link

Why the % variable is not working as ! variable in my batch file?

I am using array in my for loop and trying to assign the value to a variable when each time loop process the array value. but when I am assigning the value to my variable, then the result is not as expected.
Here is what I am trying to do:
#echo off
setlocal enabledelayedexpansion
SET var[0]=a
SET var[1]=b
SET var[2]=c
SET var[3]=d
for %%a in (0,1,2,3) do (
SET name=%var[%%a]%
ECHO %name%
)
but the result is showing the last value of array which is 'd'. I don't want to use ECHO !name!. And expecting the output as below with %
a
b
c
d
Normally it would shown ECHO is off. because %name% wasn't declared before. Let's say that you set the default value to null for %name%, then the output would be null instead of a, b, c and d. That's because %name% was declared outside the loop.
What is going on here? The echo %name% inside the code block will get the variable %name% first before run it, and therefore your code inside code block after parsed would be:
(
set name=
rem "%var[%" = "" and "%a]%" also = ""
echo
rem %name% wasn't here, because it's parsed before you run it.
)
The ! symbol is the alternative variable pre/suffix, and only works if you use
setlocal EnableDelayedExpansion
Variable that's use ! will only get parsed when the statement is actually running instead of parsing:
(
set name=!var[%%a]!
echo !name!
)
This time, the code block would ignore the ! symbol and only looking for % symbol, and because the for loop, %%a will replace by a number:
(
set name=!var[0]!
echo !name!
)
(
set name=!var[1]!
echo !name!
)
(
set name=!var[2]!
echo !name!
)
(
set name=!var[3]!
echo !name!
)
Next time, if you want to change variable and use it, you might have to use ! instead of % otherwise you'll get repeated result. And if you want to echo the ! symbol, you can just escape it:
echo escaped "^!" symbol^!

how to match the command line arguments in the batch file

The following script commands check matching the command line argument %1 against the fixed word ala
<code>
#echo off
set one=%1
set two=%2
If NOT "%one%"=="%one:ala=%" ( echo the first argument contains the word "ala")
else ( echo no matching ! )
</code>
How to replace the fixed word "ala" with an argument %2 from the command line instead.
(because the simple replacement ala with %2 doesnt work).
Is there any better solution for comparing the argument strings ?
#ECHO OFF
SETLOCAL
ECHO %~1|FIND "%~2">NUL
IF ERRORLEVEL 1 (
ECHO "%~2" NOT found IN "%~1"
) ELSE (
ECHO "%~2" WAS found IN "%~1"
)
GOTO :EOF
Use the find facility. This avoids delayedexpansion but is relatively slow.
You need to use delayed expansion to accomplish that type of string replacement.
#echo off
setlocal enabledelayedexpansion
set "one=%~1"
set "two=%~2"
If NOT "%one%"=="!one:%two%=!" (
echo the first argument contains the word "%two%"
) else (
echo no matching
)
And you could also do it without delayed expansion using a technique with the CALL command.
#echo off
set "one=%~1"
CALL set "two=%%one:%~2=%%"
If NOT "%one%"=="%two%" (
echo the first argument contains the word "%two%"
) else (
echo no matching
)

If else issue with arrays in batch

I have created two sorted arrays of size n and m with numeric value in a batch file.
FOR /L %%a IN (0,1,!n!) DO ECHO !vector[%%a]!
FOR /L %%a IN (0,1,!m!) DO ECHO !vector2[%%a]!
This exactly shows the contents of my arrays.
Now I want to write a logic which will print merged sorted array.
SETLOCAL ENABLEEXTENSIONS ENABLEDELAYEDEXPANSION
set i=0
set j=0
set /A totalElements =!n!+!m!
FOR /L %%A IN (1,1,!totalElements!) DO (
if !vector[!i!]! LSS !vector2[!j!]! (
echo "First list"
echo !%vector[!i!]%!
) else (
echo "Second List"
echo !%vector[!i!]%!
)
)
So, this if else logic is not working. Any idea where in the syntax I have gone wrong? I guess I am not extracting the value from the array correctly?
Incomplete analyse:
where are defined n and m variables in set /A totalElements =!n!+!m! ?
in !vector[!i!]!, batch parser is looking for variables !vector[! and !]! instead of nesting like in !vector[%i%]!;
!%vector[!i!]%! is totally unclear for me.
Your question seems to be too broad (a bit unclear aim) to give more positive and constructive notes.

Batch file doesn't work, how can I to fix it?

I want to write a game using a batch script, but there is an error and I don't know what to do.
#echo on
goto log
:inicio
set "esquerda=caverna"
set "esquerda2=6"
set /p "run=Digite a acao: "
if %run%==help (
goto help) else (
goto comands)
:carvena
echo oi>"%userprofile%\desktop\oi.txt"
pause>nul
:comands
echo %run% >"%tmp%\reino_de_merlock\personagem\comands.txt"
cd "%tmp%\reino_de_merlock\personagem"
type comands.txt | find /i "andar_esquerda" && echo andar>andar_esquerda.txt
if exist andar_esquerda.txt (
set "andar=esquerda1"
del /q andar_esquerda.txt
goto andar1)
:log
for /f %%a in ('type "%tmp%\reino_de_merlock\personagem\agilidade.txt"') do (
set "agilidade=%%a" & goto inicio)
:andar1
if "%andar%" == "esquerda1" (
set /a andar = agilidade - esquerda2
if %andar% LEQ 0 (
goto %esquerda%)) 1>nul 2>nul
pause>nul
The error is in set /a andar = agilidade - esquerda2, the variable becomes esquerda1 and the right it's -2, what is the error?
Within a block statement (a parenthesised series of statements), the entire block is parsed and then executed. Any %var% within the block will be replaced by that variable's value at the time the block is parsed - before the block is executed.
Hence, IF (something) else (somethingelse) will be executed using the values of %variables% at the time the IF is encountered.
Two common ways to overcome this are 1) to use setlocal enabledelayedexpansion and use !var! in place of %var% to access the changed value of var or 2) to call a subroutine to perform further processing using the changed values.
In your case,
if %andar% LEQ 0 (
goto %esquerda%)
is contained within the block that starts
if "%andar%" == "esquerda1" (
and consequently uses the values of andar and esquerda1 as they were when the if "%andar%" == "esquerda1" was reached, not on the new value of andar calculated within the loop.
Look for many SO entries on 'delayed expansion'

Resources