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-08-03 09:29
中国DOS联盟论坛 » DOS批处理 & 脚本技术(批处理室) » How can I use batch processing to calculate how many lines there are in a text file? View 1,014 Replies 14
Original Poster Posted 2007-02-07 03:09 ·  中国 吉林 长春 电信
初级用户
Credits 39
Posts 16
Joined 2007-01-30 05:29
19-year member
UID 78088
Gender Male
Status Offline
How can I use batch processing to calculate how many lines there are in a text file? Then use that total line count to generate a random number and display a random line.

How exactly should I write the statement...
Floor 2 Posted 2007-02-07 03:35 ·  中国 河北 廊坊 三河市 移动
金牌会员
★★★★
Credits 2,725
Posts 1,160
Joined 2006-09-23 12:00
19-year member
UID 63486
From 河北廊坊
Status Offline
Using findstr /n will tell you how many lines a file has. For details, type findstr /? at the command line to check.
To see how many lines are in the file test.txt, if you use a for statement, you can do it like this:

@echo off 
for /f "tokens=1 delims=:" %%i in ('findstr /n .* test.txt') do set num=%%i
echo There are %num% lines in file test.txt
pause>nul
三人行,必有吾师焉。 学然后知不足,教然后知困,然后能自强也。
Floor 3 Posted 2007-02-07 04:16 ·  中国 广东 电信
荣誉版主
★★★★
batch fan
Credits 5,226
Posts 1,737
Joined 2006-03-10 00:38
20-year member
UID 51697
From 成都
Status Offline
  There was an error when merging; the OP is theoldman, not ccwan. Please forgive me, both of you, and everyone else please bear with it.
  ——————————————————————————
  The problem of misaligned post numbers has already been fixed by the admin. Everyone below, please make the corresponding changes to your reply content in time.

[ Last edited by namejm on 2007-2-6 at 04:11 PM ]
尺有所短,寸有所长,学好CMD没商量。
考虑问题复杂化,解决问题简洁化。
Floor 4 Posted 2007-02-07 04:17 ·  中国 河北 廊坊 三河市 移动
金牌会员
★★★★
Credits 2,725
Posts 1,160
Joined 2006-09-23 12:00
19-year member
UID 63486
From 河北廊坊
Status Offline
Moderator, you're too modest. Actually, being the OP is pretty nice too, I just don't know whether the credits would accordingly go up by 4 points instead of 2?^_^

[ Last edited by ccwan on 2007-2-7 at 04:22 AM ]
三人行,必有吾师焉。 学然后知不足,教然后知困,然后能自强也。
Floor 5 Posted 2007-02-07 04:22 ·  中国 北京 东城区 联通
金牌会员
★★★★
Credits 2,902
Posts 1,147
Joined 2006-09-21 12:00
19-year member
UID 63324
Gender Male
Status Offline
    Redtek,一个永远在网上流浪的人……

_.,-*~'`^`'~*-,.__.,-*~'`^`'~*-,._,_.,-*~'`^`'~*-,._,_.,-*~'`^`'~*-,._
Floor 6 Posted 2007-02-07 04:24 ·  中国 河北 廊坊 三河市 移动
金牌会员
★★★★
Credits 2,725
Posts 1,160
Joined 2006-09-23 12:00
19-year member
UID 63486
From 河北廊坊
Status Offline
三人行,必有吾师焉。 学然后知不足,教然后知困,然后能自强也。
Floor 7 Posted 2007-02-07 04:30 ·  中国 吉林 长春 电信
初级用户
Credits 39
Posts 16
Joined 2007-01-30 05:29
19-year member
UID 78088
Gender Male
Status Offline
ccwan, if I want to display line 2, and this 2 is a variable, what exactly should the format be?
(Please forgive a newbie like me for asking such a simple question, I really couldn't find how to write it T_T~~)

[ Last edited by namejm on 2007-2-6 at 03:52 PM ]
Floor 8 Posted 2007-02-07 04:42 ·  中国 广东 电信
荣誉版主
★★★★
batch fan
Credits 5,226
Posts 1,737
Joined 2006-03-10 00:38
20-year member
UID 51697
From 成都
Status Offline
  The demo code is as follows:

@echo off
set num=
set /p num= Please enter the line number to display:
set /a num-=1
for /f "delims= eol=" %%i in ('more +%num% test.txt') do echo "%%i"&goto end

:end
pause

  You can also use findstr /n, or display it through a counter.
Recent Ratings for This Post ( 2 in total) Click for details
RaterScoreTime
ccwan +5 2007-02-07 04:43
theoldman +2 2007-02-07 04:51
尺有所短,寸有所长,学好CMD没商量。
考虑问题复杂化,解决问题简洁化。
Floor 9 Posted 2007-02-07 04:50 ·  中国 吉林 长春 电信
初级用户
Credits 39
Posts 16
Joined 2007-01-30 05:29
19-year member
UID 78088
Gender Male
Status Offline
Moderator, what does this more do?
Floor 10 Posted 2007-02-07 05:00 ·  中国 广西 玉林 博白县 电信
金牌会员
★★★★
Credits 3,687
Posts 1,467
Joined 2005-08-08 12:00
20-year member
UID 44210
Status Offline
Use more/? and command /? more often in the CMD window.
Floor 11 Posted 2007-02-07 05:01 ·  中国 河北 廊坊 三河市 移动
金牌会员
★★★★
Credits 2,725
Posts 1,160
Joined 2006-09-23 12:00
19-year member
UID 63486
From 河北廊坊
Status Offline
Just do as the poster above said.

[ Last edited by ccwan on 2007-2-7 at 05:06 AM ]
三人行,必有吾师焉。 学然后知不足,教然后知困,然后能自强也。
Floor 12 Posted 2007-02-07 05:05 ·  中国 广西 玉林 博白县 电信
金牌会员
★★★★
Credits 3,687
Posts 1,467
Joined 2005-08-08 12:00
20-year member
UID 44210
Status Offline
ccwan, your post on reply #4 is really hard on the eyes.
Floor 13 Posted 2007-02-07 05:09 ·  中国 河北 廊坊 三河市 移动
金牌会员
★★★★
Credits 2,725
Posts 1,160
Joined 2006-09-23 12:00
19-year member
UID 63486
From 河北廊坊
Status Offline
Sorry everyone, the starting color and the ending color were set too light. You can select it to reverse it to white on black and it'll be clear.

Also, I suggest the OP read the basic command help more.
三人行,必有吾师焉。 学然后知不足,教然后知困,然后能自强也。
Floor 14 Posted 2007-02-07 05:24 ·  中国 甘肃 兰州 电信
金牌会员
★★★★
Credits 4,103
Posts 1,744
Joined 2006-01-20 13:00
20-year member
UID 49241
Gender Male
From 甘肃.临泽
Status Offline
Floor 15 Posted 2007-02-07 05:30 ·  中国 吉林 长春 电信
初级用户
Credits 39
Posts 16
Joined 2007-01-30 05:29
19-year member
UID 78088
Gender Male
Status Offline
Thanks to the helpful people above s ^_^

If I still can't understand it, I'll come back and bother everyone again.
Forum Jump: