10

In one of my batch files I'm attempting to write, I need to read the last line in a 5 line text file into a variable. One way I thought of doing this might be to have each line that is put into the text file overwrite the previous (as the files creation and lines are all created by a command, specifically slmgr /xpr) but I'm not sure how to do this. Any suggestions or alternate ways of doing this are appreciated!

cscript %windir%\system32\slmgr.vbs /xpr > xprtest.txt
pause
for /F "skip=4 delims=" %i in (xprtest.txt) set /p xprvar= <xprtest.txt
pause
echo %xprvar%
user2997654
  • 157
  • 2
  • 2
  • 6

2 Answers2

14

if you want specifically line 5:

set "xprvar="
for /F "skip=4 delims=" %%i in (xprtest.txt) do if not defined xprvar set "xprvar=%%i"

OR

for /F "skip=4 delims=" %%i in (xprtest.txt) do set "xprvar=%%i"&goto nextline
:nextline

if you want the last line regardless of the number of lines:

for /F "delims=" %%i in (xprtest.txt) do set "xprvar=%%i"
Magoo
  • 77,302
  • 8
  • 62
  • 84
  • This was useful thank you. However I had to add type in front of the file name for it to work. e.g. 'type xprtest.txt' – Dale C Jan 18 '18 at 18:22
  • @Magoo I'm trying this to get the third line in my file but it never gets it not sure what is wrong? `set "xprvar="` `for /F "skip=3 delims=" %%i in ("Z:\my folder\displayname.txt") do if not defined xprvar set "xprvar=%%i"` – Tak May 11 '18 at 18:30
  • @tak You should raise your own question. You've not stated *what* value is assigned to `xprvar` - I'd suggest that it would be the value from line 4 of your file. Notice OP here is asking for line `5` so the `skip` value is `4`. Therefore since you want line `3`, assign `2`, not `3` as the `skip` value. – Magoo May 12 '18 at 00:40
  • The `if not defined` portion was critical for output that has a trailing newline. Thanks! – tresf Dec 15 '22 at 20:25
6

The for command is quite powerful nowadays:

for /F "skip=4 delims=" %i in (test.txt) do echo %i

Skip the first 4 lines, leaving the 5th line effectively. No delimiters, so that you get the complete line and not only the first token of that line.

Replace echo %i by anything you want to do with the 5th line.

Thomas Weller
  • 55,411
  • 20
  • 125
  • 222
  • this is a test of what im trying to do. that command spits 5 lines into a text file, then if all goes correctly, takes the final line and puts it into the variable? cscript %windir%\system32\slmgr.vbs /xpr > xprtest.txt pause for /F "skip=4 delims=" %i in (xprtest.txt) set /p xprvar= – user2997654 Mar 10 '14 at 19:19
  • added the code into the OP for better veiwing. Keep getting "An unexpected error has occured" – user2997654 Mar 10 '14 at 19:25
  • @user2997654 Replace `set /p xprvar= – Thomas Weller Mar 10 '14 at 20:01
  • 2
    @user2997654 BTW: it's not nice to make major edits to the question, especially don't copy parts of an answer. Why? Because now it looks like I gave an answer which is worse than the question, so I have the risk of downvotes. – Thomas Weller Mar 10 '14 at 20:09