MarkZvu
MarkZvu

Reputation: 51

Batch script - Wrong If syntax

I am trying to check the text in my created array, if I am not using "if" every thing works and i can use "echo" but when I add the "if" command I get "wrong syntax"

@echo off
setlocal ENABLEDELAYEDEXPANSION

set i=0

for /f "delims= " %%a in ('command') do (

set /A i+=1
set list[!i!]=%%~a
)

set Filesx=%i%

rem Display array elements
for /L %%i in (1,1,%Filesx%) do (
    if list[%%i] =="some ttext"
        echo !list[%%i]!
)

Upvotes: 0

Views: 47

Answers (2)

Compo
Compo

Reputation: 38579

I would consider changing your script accordingly:

@Echo Off
SetLocal EnableDelayedExpansion

Set "i=0"
For /F %%A In ('command') Do (Set/A i+=1
    Set list[!i!]=%%~A)

Rem Display array elements
For /L %%A In (1,1,%i%) Do If /I "!list[%%A]!"=="some ttext" Echo !list[%%A]!

Pause

In your script you needed to change if list[%%i] to If /I "!list[%%i]!"

@echo off
setlocal ENABLEDELAYEDEXPANSION

set i=0

for /f "delims= " %%a in ('command') do (

    set /A i+=1
    set list[!i!]=%%~a
)

set Filesx=%i%

rem Display array elements
for /L %%i in (1,1,%Filesx%) do (
    if /i "!list[%%i]!" =="some ttext" (
        echo !list[%%i]!
    )
)

Upvotes: 1

Magoo
Magoo

Reputation: 79982

An if statement requires an exact match (for == or equ) so if you quote one side, you need to quote the other.

also, the action for the if-true condition must be on the same physical line as the if

Upvotes: 0

Related Questions