China DOS Union

-- Unite DOS · Advance DOS · Grow DOS --

Union site: www.cn-dos.net Forum site: www.cn-dos.net/forum
DOS stands for freedom, openness and progress. Let us work hard, learn from the openness and GNU spirit of FreeDOS and Linux, and together build and grow a free GNU GPL world!

中国DOS联盟论坛
The time now is 2026-06-22 17:27
中国DOS联盟论坛 » DOS批处理 & 脚本技术(批处理室) » How to use the for command to extract the last line of text content as a variable View 2,927 Replies 10
Original Poster Posted 2006-12-22 12:38 ·  中国 湖南 长沙 电信
银牌会员
★★★
Credits 2,268
Posts 879
Joined 2006-12-19 16:23
19-year member
UID 73968
Gender Male
Status Offline
The content of text a is as follows
0
1
2
3
4
5
Purpose: Run the following batch processing, %i% is equal to the content of the last line of document a,
@echo off
set /a i=%i%+1
echo %i%
Floor 2 Posted 2006-12-22 12:42 ·  中国 广西 玉林 博白县 电信
金牌会员
★★★★
Credits 3,687
Posts 1,467
Joined 2005-08-08 12:00
20-year member
UID 44210
Status Offline
for %%i in (a.txt) do echo %%i
Floor 3 Posted 2006-12-22 12:51 ·  中国 广东 电信
荣誉版主
★★★★
batch fan
Credits 5,226
Posts 1,737
Joined 2006-03-10 00:38
20-year member
UID 51697
From 成都
Status Offline
```
@echo off
for /f %%i in (a.txt) do set i=%%i
set /a i=%i%+1
echo %i%


[ Last edited by namejm on 2006-12-22 at 12:47 PM ]
Recent Ratings for This Post ( 1 in total) Click for details
RaterScoreTime
26933062 +1 2006-12-22 13:23
尺有所短,寸有所长,学好CMD没商量。
考虑问题复杂化,解决问题简洁化。
Floor 4 Posted 2006-12-22 13:30 ·  中国 湖南 长沙 电信
银牌会员
★★★
Credits 2,268
Posts 879
Joined 2006-12-19 16:23
19-year member
UID 73968
Gender Male
Status Offline
Thanks to the user on floor 3, it seems that you need to add /f after for. Also, thanks to the brother on floor 2. Why didn't you all add /f? According to what you said, what's displayed is the file name a.txt instead of the last line of the document content. Did I not make my question clear?
Floor 5 Posted 2006-12-22 14:09 ·  中国 广西 玉林 博白县 电信
金牌会员
★★★★
Credits 3,687
Posts 1,467
Joined 2005-08-08 12:00
20-year member
UID 44210
Status Offline
Just wrote it casually, forgot to add "/f"
Floor 6 Posted 2006-12-23 01:48 ·  中国 广东 电信
荣誉版主
★★★★
batch fan
Credits 5,226
Posts 1,737
Joined 2006-03-10 00:38
20-year member
UID 51697
From 成都
Status Offline
Hehe, I wrote it casually based on the 2F, but unexpectedly made a junior mistake.
尺有所短,寸有所长,学好CMD没商量。
考虑问题复杂化,解决问题简洁化。
Floor 7 Posted 2006-12-23 03:33 ·  中国 湖北 武汉 电信
版主
★★★★★
Credits 11,386
Posts 4,938
Joined 2006-07-23 17:10
19-year member
UID 59080
Status Offline
Originally posted by namejm at 2006-12-21 23:51:

@echo off
for /f %%i in (a.txt) do set i=%%i
set /a i=%i%+1
echo %i%


[ Last edited by namejm on 2006-12-22 at 12:47 PM ]


  Don't understand why it's written like this. What's the purpose of adding 1?

@echo off
for /f "delims=" %%i in (a.txt) do set i=%%i
echo %i%
pause
Floor 8 Posted 2006-12-23 03:45 ·  中国 河北 廊坊 三河市 移动
金牌会员
★★★★
Credits 2,725
Posts 1,160
Joined 2006-09-23 12:00
19-year member
UID 63486
From 河北廊坊
Status Offline
Adding 1 is the purpose that the LZ's batch processing needs to accomplish.
三人行,必有吾师焉。 学然后知不足,教然后知困,然后能自强也。
Floor 9 Posted 2006-12-23 03:47 ·  中国 湖北 武汉 电信
版主
★★★★★
Credits 11,386
Posts 4,938
Joined 2006-07-23 17:10
19-year member
UID 59080
Status Offline

  Oh, I thought it was only extracting the last row. I read the question wrong.
Floor 10 Posted 2008-01-01 18:46 ·  中国 广东 广州 天河区 电信
初级用户
Credits 70
Posts 38
Joined 2007-05-23 07:20
19-year member
UID 89024
Gender Male
Status Offline
Ask the expert, if I want to extract the elements of the first line and the second line in front, how to do it with the program
Floor 11 Posted 2008-01-02 13:46 ·  中国 湖北 武汉 电信
版主
★★★★★
Credits 11,386
Posts 4,938
Joined 2006-07-23 17:10
19-year member
UID 59080
Status Offline
Originally posted by Li103Z at 2008-1-1 18:46:
Ask the experts, if I want to extract the elements of the first and second lines, how to do it with the program

Search for "ReadLine".
Forum Jump: