Put variable in options of for loop, for "skip=" value, but

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
pditty8811
Posts: 184
Joined: 21 Feb 2013 15:54

Put variable in options of for loop, for "skip=" value, but

#1 Post by pditty8811 » 22 Mar 2013 14:40

I am trying to skip lines in order to grab var5 in the second line below this findstr command:

Code: Select all

'findstr /x /v /c:"Ship sunk" "C:\Users\P Ditty\Documents\SH3\data\cfg\Backups_SCR2\!FileName5!.clg" "C:\Users\P Ditty\Documents\SH3\data\cfg\Backups_SCR2\!FileName5!.cfg"')


Repeat, I'd like to grab var5 from the second line below where the findstr (above) finds its string.

So I decided I would create a variable for the skip in the for loop. I called it "skipnumber5". However, using this variable it skips the amount of lines that I say, but then it doesn't repeat the loop. So if I set skipnumber5=12 it does not act the same as "skip=12". It does the first loop correctly, but then it repeats the first loop result again and again. Using skip=12 does work correctly with the loop,but I need skip value to change in order to grab var5 as it changes.

Code:

Code: Select all

 set /a skipnumber5=0
:part5
set /a skipnumber5+=12
set /a maxlines5+=1
set /a linecount5=0

for /F "delims=" %%x in ('findstr /c:"Ship sunk" "C:\Users\P Ditty\Documents\SH3\data\cfg\Backups_SCR2\*.clg" "C:\Users\P Ditty\Documents\SH3\data\cfg\Backups_SCR2\Log_*.cfg"') do (
set /a linecount5+=1
set FileName5=
set FileName5=%%~nx
if !linecount5! GEQ %maxlines5% goto part5a
)

:part5a
set /a maxlines5a+=1
set /a linecount5a=0
for /f "skip=%skipnumber5% tokens=2 delims==" %%E in ('findstr /x /v /c:"Ship sunk" "C:\Users\P Ditty\Documents\SH3\data\cfg\Backups_SCR2\!FileName5!.clg" "C:\Users\P Ditty\Documents\SH3\data\cfg\Backups_SCR2\!FileName5!.cfg"') do (
set /a linecount5a+=1
set var5=
set var5=!var5!%%E
set /a skipnumber5+=12
if !linecount5a! GEQ %maxlines5a% (goto part5b)
)


Line in question with variable skipnumber5, in for loop options, is:

Code: Select all

 for /f "skip=%skipnumber5% tokens=2 delims==" %%E in


The problem is that the skip with variable skipnumber5 is staying in the same file, skipping. But if you read the code it is supposed to change to the next Filename5, skipping. That is how it works if skip=1, or any integer.

So why is the variable skipnumber5 making skip= act this way?

!k
Expert
Posts: 378
Joined: 17 Oct 2009 08:30
Location: Russia

Re: Put variable in options of for loop, for "skip=" value,

#2 Post by !k » 24 Mar 2013 06:09

Code: Select all

for /? >my.txt


for /f "delims=:" %%a in ('findstr /n /c:"skip=" my.txt') do set /a n=%%a+1
rem echo n = "%n%"
for /f "skip=%n% tokens=2 delims== " %%b in ('type my.txt') do (
   set "var=%%b"
   goto :next
)
:next


echo var = "%var%"
pause

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Put variable in options of for loop, for "skip=" value,

#3 Post by foxidrive » 24 Mar 2013 06:18

pditty8811 wrote: The problem is that the skip with variable skipnumber5 is staying in the same file, skipping. But if you read the code it is supposed to change to the next Filename5, skipping. That is how it works if skip=1, or any integer.

So why is the variable skipnumber5 making skip= act this way?


The for-in-do command parameters are most likely parsed at runtime and cannot be changed within the loop as you seem to be doing.

pditty8811
Posts: 184
Joined: 21 Feb 2013 15:54

Re: Put variable in options of for loop, for "skip=" value,

#4 Post by pditty8811 » 24 Mar 2013 13:06

So I have to assign the variable outside of the loop, in order for it to work?

mfm4aa
Posts: 70
Joined: 13 Feb 2013 14:02
Location: Europe

Re: Put variable in options of for loop, for "skip=" value,

#5 Post by mfm4aa » 24 Mar 2013 13:30

pditty8811 wrote:So I have to assign the variable outside of the loop, in order for it to work?

Yes, as I did say before.

Post Reply