1
@echo off
setlocal enabledelayedexpansion
set chk=tru
set final=c:\users\user1\desktop
set var=c:\users\user1\desktop
    for %%s in (%final%) do (
        echo %%s
        echo %var%
        if %var%==%%s set chk=false
        echo %chk%
    )

the two strings are the same but the if statment wont excute the set command and change the chk variable. can someone please help? im new to batch scripting and i dont understand why this dosent work. i think its a technical matter i just dont know about

RedBelly
  • 89
  • 1
  • 2
  • 7

2 Answers2

0

in for loop use exclamation marks to reference variable. try this:

@echo off
setlocal enabledelayedexpansion
set chk=tru
set final=c:\users\user1\desktop
set var=c:\users\user1\desktop
    for %%s in (%final%) do (
        echo %%s
        echo %var%
        if {%var%}=={%%s} set chk=false
        echo !chk!
    )

and read about EnableDelayedExpansion

rostok
  • 2,057
  • 2
  • 18
  • 20
0

Since you are changing the value of chk in the loop itself the changes made gets reflected back only in the next iteration. Thus you need to use ! instead of % to see the change immediately.

@echo off
setlocal enabledelayedexpansion
set chk=tru
set final=c:\users\user1\desktop
set var=c:\users\user1\desktop
    for %%s in (%final%) do (
        echo %%s
        echo %var%
        if %var%==%%s set chk=false
        echo !chk!
    )
Rishav
  • 3,818
  • 1
  • 31
  • 49