1

I have the following code script:

set "file=%cd%/Config.mak"
set /a i=0
set /a j=0

@REM Delete the file if it currently exists in the folder:
IF EXIST Disabled_Features.txt del /F Disabled_Features.txt
@REM If the file can't be deleted, exit with an error:
IF EXIST Disabled_Features.txt exit 1

setlocal EnableDelayedExpansion EnableExtensions

@REM Parse Features from Config.xml into string array.
for /f "usebackq delims=" %%a in ("%file%") do (
    call set /a i+=1
    call set Feature[%i%]=%%a
)   

@REM Regexr parametre
call set "search=:=$"

@REM Keep the Disabled Features into new array for stripping module:
for /L %%N in (1,1,%i%) do (
    echo(!Feature[%%N]!|findstr /R /C:"%search%">nul && (
        call echo FOUND
        call set /A j+=1
        call set Feature_Disabled[%j%]=!Feature[%%N]:~0,-2!
        call echo.!Feature_Disabled[%j%]!>>Disabled_Features.txt
    ) || (
        call echo NOT FOUND 
    )
) 
endlocal

I was able to print and acces the first "for" instruction array values, so it means they are correctly gathered from the file, but when it comes to the second "for" instruction, the instruction !Feature[%%N]! doesn't seem to pass a correct value to the findstr, since the regular expression is ':=$', which matches the characters ':=' at the end of the line, and the array values are of the form:

EXAMPLE_SAMPLE1:=x5
EXAMPLE_SAMPLE2:=
EXAMPLE_SAMPLE3:=asdadasd

So in this case the findstr should match the second value of the string array.

Whenever I try the command (to debug):

 call echo.!Feature[%%N]!

the output shows:

!Feature[1]!

Any pointing to a proper solution for this?

Thank you a lot.

Aacini
  • 65,180
  • 12
  • 72
  • 108
Jackson
  • 101
  • 10
  • 1
    [delayed expansion](http://ss64.com/nt/delayedexpansion.html) – npocmaka Feb 03 '17 at 10:19
  • My expansion ia already set – Jackson Feb 03 '17 at 11:21
  • 3
    when you want to use delayed expansion you need to enclose variables like this `!variable!` instead `%variable%` – npocmaka Feb 03 '17 at 12:06
  • 2
    Use either `set "Feature[!i!]=%%a"` or `call set "Feature[%%i%%]=%%a"` (the latter one does not require enabled delayed expansion). – JosefZ Feb 03 '17 at 12:37
  • Your method should work. If you want to test that the last two chars in the array elements be `:=`, then it is _much_ simpler to use: `if "!Feature[%%N]:~-2!" equ ":=" ( then part ) else ...`. You may also eliminate most `call` commands, excepting in `call set Feature[%%i%%]=%%a` line with _double_ percent signs (as JosefZ indicated), but you may use `!i!` here and the same with `[%j%]` below. See [this answer](http://stackoverflow.com/questions/10166386/arrays-linked-lists-and-other-data-structures-in-cmd-exe-batch-script/10167990#10167990). – Aacini Feb 03 '17 at 15:50
  • Thanks, if you post it as an answer I'll mark it as correct. – Jackson Feb 13 '17 at 07:33

0 Answers0