1

Here is my scenario : I make a batch to recover some number line from a file.txt, after set each line in a variable. And after that I want to use the different variable, dynamically created, in a git command.

I have really advanced (thanks to this site in particular) and close to the final goal. Let's imagine my file.txt look like this and called errorLine.txt:

365
369
389
393
413
417
437
491
515
566
587
610
681
887
964

And my script here (blame.bat): EDITED

FINDSTR /R /N "^.*" ..\log_test\errorLine.txt | FIND /C ":" > ..\log_test\numbLine.txt
(set /p i=)<..\log_test\numbLine.txt
echo %i%
setlocal EnableDelayedExpansion

rem Load the lines of file in "l" array:
set n=0
for /F %%a in (..\log_test\errorLine.txt) do (
   set /A n+=1
   set "l!n!=%%a"
)

rem Show any line you want, for example:
set n=1
git blame -L !l%n%!,+1 ..\..\core\src\filepath.cpp -e
set n=2
git blame -L !l%n%!,+1 ..\..\core\src\filepath.cpp -e

pause

That's working but is there a way to not have to set for each line at the end (set n=1, set n=2, ...) and increment in link of the number of line ?

Thanks in advance.

Algorys
  • 1,710
  • 2
  • 25
  • 52

2 Answers2

1

Excuse me. The way to solve your problem is insert

setlocal EnableDelayedExpansion

at beginning of your program and then use this:

echo '!l%%n!'

However, this is the way I would do that:

@echo off
setlocal EnableDelayedExpansion

rem Load the lines of file in "l" array:
set n=0
for /F %%a in (..\log_test\errorLine.txt) do (
   set /A n+=1
   set "l!n!=%%a"
)

rem Show any line you want, for example:
set n=15
echo '!l%n%!'

For further details, see this post.

Community
  • 1
  • 1
Aacini
  • 65,180
  • 12
  • 72
  • 108
  • yes.This is easier ,but I proffered to follow the OP code. – npocmaka Feb 10 '15 at 15:51
  • OK, i will try anyway because, behind when i make that `git blame -L !l%%n!,+1 ..\..\core\src\filepath.cpp -e` that fails and don't display the number line... :( – Algorys Feb 10 '15 at 15:52
  • @Aacini I update my post, if you can continue to help me please ? – Algorys Feb 10 '15 at 16:20
  • @Aacini I used your answer, but if you have a solution to increment each `set`after please ? (see my edit) – Algorys Feb 10 '15 at 16:52
0
FINDSTR /R /N "^.*" ..\log_test\errorLine.txt | FIND /C ":" > ..\log_test\numbLine.txt
(set /p i=)<..\log_test\numbLine.txt
echo %i%
set n=1
setlocal enableDelayedExpansion
for /L %%n in (1,1,%i%) do (
    grep -m%%n "[0-9][0-9][0-9]" ..\log_test\errorLine.txt | tail -n1 > ..\log_test\line%%n.txt
    (set /p l%%n=)<..\log_test\line%%n.txt
    echo '!l%%n!'
    )
echo "%l15%"
cd 
pause
endlocal

May be this is what you looking for.You need delayed expansion and %%n should be one side enclosed with %.But I have no grep and tee installed so not sure if this will work.

npocmaka
  • 55,367
  • 18
  • 148
  • 187
  • Thanks for the tip, but I'm looking for display the value of `l15` (or another one)... And here it just display '15'. I think your solution is not so far... – Algorys Feb 10 '15 at 15:23
  • @mestra - what is the content of `line15.txt` ? – npocmaka Feb 10 '15 at 15:35
  • the content of line15.txt is 964 (like l15 in fact) and the line14.txt is 887 (like l14), etc... – Algorys Feb 10 '15 at 15:37
  • Ok, i get it. The solution is `echo '!l%%n!'` and it display '964' as expected. Thanks @npocmaka found from [here] (http://stackoverflow.com/questions/24234994/batch-script-create-dynamic-variable-names-or-array-in-for-loop) – Algorys Feb 10 '15 at 15:46