0

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]!
)
2
  • Shouldn't there be parentheses around the echo !list[%%i]! command, too? Commented Oct 8, 2017 at 14:07
  • dont thinks so, anyway, i added them and the result is the same Commented Oct 8, 2017 at 14:09

2 Answers 2

1

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]!
    )
)
Sign up to request clarification or add additional context in comments.

Comments

0

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

1 Comment

and with my addendum...??

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.