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.

2 Answers 2

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.

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

4 Comments

yes.This is easier ,but I proffered to follow the OP code.
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... :(
@Aacini I update my post, if you can continue to help me please ?
@Aacini I used your answer, but if you have a solution to increment each setafter please ? (see my edit)
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.

4 Comments

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...
@mestra - what is the content of line15.txt ?
the content of line15.txt is 964 (like l15 in fact) and the line14.txt is 887 (like l14), etc...
Ok, i get it. The solution is echo '!l%%n!' and it display '964' as expected. Thanks @npocmaka found from [here] (stackoverflow.com/questions/24234994/…)

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.