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-07-29 18:42
中国DOS联盟论坛 » DOS批处理 & 脚本技术(批处理室) » How to display the total number of lines in a text View 2,909 Replies 17
Original Poster Posted 2007-02-26 09:07 ·  中国 北京 中国科学院研究生院
银牌会员
★★★
Credits 1,187
Posts 555
Joined 2006-12-21 07:35
19-year member
UID 74129
Gender Male
Status Offline
As the title says
Floor 2 Posted 2007-02-26 09:31 ·  中国 北京 中国科学院研究生院
银牌会员
★★★
Credits 1,187
Posts 555
Joined 2006-12-21 07:35
19-year member
UID 74129
Gender Male
Status Offline
for /f "delims=" %%i in (l.txt) do (set/a num+=1)

echo len=%num%

But if there are carriage return symbols in the line, the result will be incorrect. How to modify it, thanks!
Floor 3 Posted 2007-02-26 09:48 ·  中国 宁夏 银川 电信
金牌会员
★★★★
Credits 4,103
Posts 1,744
Joined 2006-01-20 13:00
20-year member
UID 49241
Gender Male
From 甘肃.临泽
Status Offline
See if you need blank lines and lines starting with ;
Floor 4 Posted 2007-02-26 09:57 ·  中国 北京 中国科学院研究生院
银牌会员
★★★
Credits 1,187
Posts 555
Joined 2006-12-21 07:35
19-year member
UID 74129
Gender Male
Status Offline
All need. As long as there are characters in a line, no matter what characters they are. Including lines with only one carriage return character.

[ Last edited by scriptor on 2007-2-25 at 09:42 PM ]
Floor 5 Posted 2007-02-26 11:00 ·  中国 宁夏 银川 电信
金牌会员
★★★★
Credits 4,103
Posts 1,744
Joined 2006-01-20 13:00
20-year member
UID 49241
Gender Male
From 甘肃.临泽
Status Offline
Originally posted by scriptor at 2007-2-26 09:57:
All are required. As long as there are characters in a line, no matter what characters they are. Including lines with only a carriage return character.

[ Last edited by scriptor on 2007-2-25 at 09:42 PM ]

Then still use sed. sed -n $= life. Because when there are blank lines in the file, using findstr /N . life will still ignore blank lines.
Floor 6 Posted 2007-02-26 11:23 ·  中国 北京 中国科学院研究生院
银牌会员
★★★
Credits 1,187
Posts 555
Joined 2006-12-21 07:35
19-year member
UID 74129
Gender Male
Status Offline
The content of my l.txt is:
w

e
The content of my bat file is:
@echo off
for /f "delims=" %%i in ('find /v /i /n "" l.txt') do (set/a num+=1)
echo len=%num%
pause>nul

The result is 4, but there are only three lines??
Does it have to be subtracted by 1 like this to be correct? What's the reason for this?
Are there other methods?
Floor 7 Posted 2007-02-26 11:45 ·  中国 陕西 西安 电信
铂金会员
★★★★
Credits 5,212
Posts 2,478
Joined 2007-02-08 23:39
19-year member
UID 79003
Gender Male
Status Offline
Floor 8 Posted 2007-02-26 11:53 ·  中国 北京 中国科学院研究生院
银牌会员
★★★
Credits 1,187
Posts 555
Joined 2006-12-21 07:35
19-year member
UID 74129
Gender Male
Status Offline
You mean using VBS or JS?

Of course that's possible. But what I want is bat.
Floor 9 Posted 2007-02-26 23:07 ·  中国 安徽 马鞍山 电信
中级用户
★★
Credits 493
Posts 228
Joined 2007-02-16 00:38
19-year member
UID 79596
Gender Male
From 安徽
Status Offline
@echo off
setlocal enabledelayedexpansion
set i=0
for /f %%a in (1.txt) do (
set /a i+=1
echo !i! %%a >>1new.txt
)

Cannot include blank lines!

[ Last edited by xycoordinate on 2007-2-26 at 10:10 AM ]
Floor 10 Posted 2007-02-26 23:52 ·  中国 山东 济南 电信
初级用户
Credits 125
Posts 44
Joined 2007-01-24 15:31
19-year member
UID 77555
Gender Female
Status Offline
Brother Scriptor,

Here's a weird method!! Haha, take advantage of a small bug in SET /A.


@ECHO %DBG% OFF & SETLOCAL ENABLEDELAYEDEXPANSION

FOR /F %%i IN ('findstr /n . l.txt') DO SET /A LEN=%%i 2>nul
ECHO %LEN%
Recent Ratings for This Post ( 3 in total) Click for details
RaterScoreTime
zh159 +3 2007-02-27 00:03
xycoordinate +2 2007-05-05 10:48
scriptor +4 2007-09-09 13:17
Floor 11 Posted 2007-02-27 00:01 ·  中国 湖北 武汉 硚口区 电信
版主
★★★★★
Credits 11,386
Posts 4,938
Joined 2006-07-23 17:10
20-year member
UID 59080
Status Offline

It can handle blank lines and lines containing other characters:
@echo off
for /f "tokens=1 delims=:" %%a in ('findstr /n .* 1.txt') do set a=%%a
echo The length of file 1.txt is %a%
pause&exit/b0
Recent Ratings for This Post ( 3 in total) Click for details
RaterScoreTime
xycoordinate +4 2007-05-05 10:46
scriptor +6 2007-09-09 13:17
dgoogd +1 2008-08-19 08:30
Floor 12 Posted 2007-02-27 00:02 ·  中国 广西 玉林 博白县 电信
金牌会员
★★★★
Credits 3,687
Posts 1,467
Joined 2005-08-08 12:00
20-year member
UID 44210
Status Offline
The effect of floor 10 is the same as:
@echo off
FOR /F "delims=:" %%i IN ('findstr /n . a.txt') DO SET LEN=%%i
ECHO %LEN%
pause

It uses set/a to remove the ":" character after the number.

SET LEN=%%i: Set LEN equal to the number of lines displayed by findstr /n (no need to count), including all lines

[ Last edited by zh159 on 2007-2-26 at 11:04 AM ]
Floor 13 Posted 2007-05-05 10:47 ·  中国 安徽 马鞍山 电信
中级用户
★★
Credits 493
Posts 228
Joined 2007-02-16 00:38
19-year member
UID 79596
Gender Male
From 安徽
Status Offline
Originally posted by zh159 at 2007-2-27 00:02:
The effect of floor 10 is the same as:
@echo off
FOR /F "delims=:" %%i IN ('findstr /n . a.txt') DO SET LEN=%%i
ECHO %LEN%
pause

It uses set/a to remove the ":" character after the number.
SET LEN=%%i: Set LEN equal to the number of lines displayed by findstr /n (without counting), including all lines


For floor 10, mainly used 2>nul, wonderful!!!

[ Last edited by xycoordinate on 2007-5-5 at 10:49 AM ]
Floor 14 Posted 2007-12-24 01:10 ·  中国 陕西 汉中 电信
新手上路
Credits 19
Posts 5
Joined 2007-10-10 15:51
18-year member
UID 99367
Gender Male
Status Offline
look!
<a href="http://www.cn-dos.net/forum/viewthread.php?tid=33878&fpage=1&highlight=%E8%A1%8C%E6%95%B0" target="_blank">How to determine the number of lines in a text in batch processing</a>
Floor 15 Posted 2007-12-24 09:03 ·  中国 四川 泸州 联通
高级用户
★★★
Credits 609
Posts 374
Joined 2006-08-02 22:38
19-year member
UID 59720
Status Offline
Don't understand? Why is it much slower than
find /v /c "" 1.txt>cs.txt
for /f "tokens=2 delims=:" %%i in (cs.txt) do @echo %%i
Forum Jump: