Solved Batch crashes despite using if defined?

June 27, 2014 at 13:44:40
Specs: Windows 64
setlocal ENABLEDELAYEDEXPANSION
set vidx=0
for /F "tokens=*" %%A in (%dr%\adirm.txt) do (
SET /A vidx=!vidx! + 1
set adm!vidx!=%%A
)
set adm


setlocal ENABLEDELAYEDEXPANSION
set vidx=0
for /F "tokens=*" %%A in (%dr%\adirb.txt) do (
SET /A vidx=!vidx! + 1
set adb!vidx!=%%A
)
set adb

if defined adb1 if not %adb1% == %adm1% echo %adb1%>>vv.txt
if defined adb2 if not %adb2% == %adm2% echo %adb2%>>vv.txt
if defined adb3 if not %adb3% == %adm3% echo %adb3%>>vv.txt
if defined adb4 if not %adb4% == %adm4% echo %adb4%>>vv.txt
if defined adb5 if not %adb5% == %adm5% echo %adb5%>>vv.txt
if defined adb6 if not %adb6% == %adm6% echo %adb6%>>vv.txt
if defined adb7 if not %adb7% == %adm7% echo %adb7%>>vv.txt

:next

--------------
There are only 3 variables(text lines) and in both adirb.txt and adirm.txt. Although %adm4%,%adm5%,%adb4%,%adb5% don't exist, the batch should just go through all the "if defined" lines and then onto :next, but for some reason it crashes in the fourth "if defined" line. Below is what the final line in the batch shows as before crashing obviosuly because there are no more variables but I assumed the "if defined" command would had allowed the batch to continue even if there are no defined variables. What am I doing wrong here? How can I fix my lines even if there are no variables defined but ensure if the variables do match , they get echoed to vv.txt? Thanks!

if defined adm11 if not == echo "">>vv.txt



See More: Batch crashes despite using if defined?

Report •


#1
June 27, 2014 at 19:04:41
✔ Best Answer
Yeh, I think you found a genuine bug. Congrats! ;-) It should perform as you intended, and as it should, but it does not. If you use ! instead of % (under delayedexpansion), or use quotes around the two var.s in the "IF", the matter is resolved, although the bug is not:
if defined adb4 if !adb4! neq !adm4! echo....
or
if defined adb4 if "%adb4%" neq "%adm4" echo...

Report •

#2
June 28, 2014 at 07:16:28
Maybe a bit less IFfy:

::====== script starts here ===============
:: compare lines in 2 txt files which may have same # lines
:: ab.bat 2014-06-28 20:54:02.94
@echo off > NEWFILE & setLocal enableDELAYedeXpansioN

:main
set N=
for /f "tokens=* delims= " %%a in (A.txt) do (
set/a N+=1
set A!N!=%%a
)
set/a MAX=N & set N=

for /f "tokens=* delims= " %%i in (B.txt) do (
set/a N+=1
set B!N!=%%i
)

if !N! lss !MAX! set MAX=!N!

for /L %%Q in (1 1 !MAX!) do (
if !A%%Q! neq !B%%Q! >> NEWFILE echo !A%%Q! !B%%Q!
)

goto :eof
::====== script ends here =================

=====================
M2 Golden-Triangle


Report •

Related Solutions


Ask Question