1


How to use a double for a looping without setting the variable for some like skip=%%L, in for loop without using configuration a set variable?

This question possibly be associated with this question, but it doesn't fit what I need. It's not the way to help me. What I am looking for is a way to use following %% loop within (inside) a For /F loop for using in a skip=%%L itinerant variable inside loop, without using none variable for skip=??_variable_??, only skip=%%L

  • Is there any way to do this?
:loop
for /f %%L in ('call set /a "_some_count_varible_-=1"') do (
    set "_for_loop_L=^tokens^=*^skip^=%%L^delims^=^ ^eol^=" && (
       for /f %_for_loop_L% %%F in ('type file_mutiplelines.log') do (
          more code here!... using %%L and %%f!

)))

I need make work like this code:

:loop
for /f %%L in ('call set /a "_some_count_varible_-=1"') do (
   for /f ^tokens^=*^skip^=%%L^delims^=^ ^eol^= %%f in ('type file_muti_lines.log') do (
      more code here!... using %%L and %%f!
))


The accepted answer results in this code:


:loop
for /f skip^=^%1^ ^tokens^=*^ ^delims^=^ ^eol^= %%F in ('type file_muti_lines.log') do (
      more code here using %1 and %%f and prevent infinit loop if "%1"=="1" goto :next
      for /f %%L in ('call set /a "_some_count_varible_-=1"')do call :loop %%L
)

Io-oI
  • 2,514
  • 3
  • 22
  • 29
  • Comments are not for extended discussion; this conversation has been [moved to chat](https://chat.stackoverflow.com/rooms/202317/discussion-on-question-by-it-wasnt-me-as-it-would-be-possible-to-use-in-a-for-l). – Samuel Liew Nov 14 '19 at 08:56

4 Answers4

2

I guess you could code the equivalent of skip=%%L inside the for loop.

setlocal enabledelayedexpansion
set _cnt=0
for /f "tokens=* delims= eol=" %%f in ('type file_muti_lines.log') do (
   set /a _cnt+=1
   if /i !_cnt! GEQ %_some_count_variable_% (
      for /f %%L in ('call set /a "_some_count_varible_-=1"') do (
         echo=;%%~L;%%f;|findstr /lvie "specific_string" && goto :next
      )
   )
)
:next

untested but I think that will skip _some_count_variable_ - 1 lines before testing for "specific_string". I am not certain that you want to goto :next after finding 1 instance of "specific_string" -- that part of your code isn't clear to me.

avery_larry
  • 2,069
  • 1
  • 5
  • 17
2

Simplest is to just skip skip altogether and use more:

@echo off
set skip=10
for /f "delims=" %%a in ('type file_mutiplelines.log ^|more +%skip%') do echo %%~a
Gerhard
  • 22,678
  • 7
  • 27
  • 43
1

Based on further comments and edited question:

set /a _some_count_variable_less_one_ = _some_count_variable_ - 1

:loop

for /f "tokens=* skip=%_some_count_variable_less_one_% delims= eol=" %%f in ('type file_mutiplelines.log') do (
   echo=;%_some_count_variable_less_one_%;%%f;|findstr /lvie "specific_string" && goto :loop
   if not "_some_count_variable_less_one_"=="!_cnt!" (set /a "_cnt+=1" && goto :loop)
   goto :next
)

:next
rem whatever happens now.

This code will produce an infinite loop when "specific_string" is found before _cnt equals _some_count_variable_less_one_.
I don't see a way to do this without defining a variable or calling a subroutine.

avery_larry
  • 2,069
  • 1
  • 5
  • 17
1

The FOR command uses a different parser than other batch commands, that results in the fact, that nor delayed expansion neither FOR-parameters expands in the options.
Your SET construct can't solve this, it will simply fail, as the probably empty variable _for_loop_L will be expanded even before you set it the first time.

Only percent expansion works, as this is evaluated before the FOR-parser will be activated.

In your case you would need

for /f %%L in ('call set /a "_some_count_varible_-=1"') do (
    call :subfunc %%L
)
exit /b

:subfunc
for /f tokens^=*^ skip^=%1^ delims^=^ eol^= %%F in ('type file_mutiplelines.log') do (
    ...
)

But using a sub function is not recommended here, as calling a function is always a slow operation.
I would recommend the solution of @avery_larry, with the IF .. GEQ ... approach

jeb
  • 78,592
  • 17
  • 171
  • 225
  • I tried to port/replace the call statement to an if, several times, but couldn't get it to automatically update the number of lines to ignore inner for. It worked perfectly using ^ skip ^ =% 1 using call in code. – Io-oI Nov 15 '19 at 01:02