Page 1 of 1

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

Posted: 22 Mar 2013 14:40
by pditty8811
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?

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

Posted: 24 Mar 2013 06:09
by !k

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

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

Posted: 24 Mar 2013 06:18
by foxidrive
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.

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

Posted: 24 Mar 2013 13:06
by pditty8811
So I have to assign the variable outside of the loop, in order for it to work?

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

Posted: 24 Mar 2013 13:30
by mfm4aa
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.