2

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.

2
  • 2
    Is this a typo: should be "!skip!" instead of "%skip%"? Commented Aug 9, 2015 at 10:42
  • 1
    Tried using that as well, it said "!skip!" was unexpected at this time." Commented Aug 9, 2015 at 11:31

1 Answer 1

3

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 .

Sign up to request clarification or add additional context in comments.

2 Comments

The file is a simple one. The first line contains an integer T i.e. the no. of test cases. T test cases follows. Each test case has 13 lines containing one integer in each.
@DarkNemesis - it's because how FOR /F parses its options.1)The parameters/tokens are always (%%A) are always expanded even after delayed expansion variables.2) When the FOR meets a %% in batch file it takes it for a parameter/token definition which is with higher prio than options - which brokes the command. Looks like command line arguments are expanded before the FOR tokens so with subroutine it works.

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.