DarkNemesis
DarkNemesis

Reputation: 109

Windows Batch File can't use variable in for syntax

I want to use a variable skip parameter in for loop, but it won't let me do it. Here is my code

@echo off
setlocal ENABLEDELAYEDEXPANSION

set /p testcase=<testcases.txt
set /a end=%testcase%*13

for /L %%P IN (1,13,%end%) DO (

    set skip=skip=%%P
    echo !skip!
    set vidx=0

    for /f "%skip%" %%A in (testcases.txt) do (
        set /a vidx=!vidx! + 1
        set var!vidx!=%%A
    )
)

Here skip is skip=1, but it doesn't skip any line. When I replace it with skip=1. then it works fine, but I want to skip variable no. of lines in each iteration. Please help.

Upvotes: 2

Views: 1070

Answers (1)

npocmaka
npocmaka

Reputation: 57252

I think with this logic the only option is a subroutine:

@echo off
setlocal ENABLEDELAYEDEXPANSION

set /p testcase=<testcases.txt
set /a end=%testcase%*13

for /L %%P IN (1,13,%end%) DO (

    set skip=skip=%%P
    echo !skip!
    set vidx=0
    call :innerFor %%P

)

exit /b 0

:innerFor
    for /f "skip=%~1" %%A in (testcases.txt) do (
        set /a vidx=!vidx! + 1
        set var!vidx!=%%A
    )
 exit /b 0

parametrization of FOR /F options is a little bit tricky.. Though I have no the content of your files I cant test if this works correctly .

Upvotes: 3

Related Questions