中国DOS联盟论坛

China DOS Union

-- Unite DOS · Advance DOS · Grow DOS --
Union site: www.cn-dos.net Forum site: www.cn-dos.net/forum
Guest | Log in | Register | Members | Search | China DOS Union
中国DOS联盟论坛
The time now is 2026-09-25 21:44
47,813 topics / 349,918 posts / today 0 new / 48,275 members
DOS批处理 & 脚本技术(批处理室) » How to display the total number of lines in a text
Printable Version  3,246 / 17
Floor1 scriptor Posted 2007-02-26 09:07
银牌会员 Posts 555 Credits 1,187
As the title says
Floor2 scriptor Posted 2007-02-26 09:31
银牌会员 Posts 555 Credits 1,187
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!
Floor3 vkill Posted 2007-02-26 09:48
金牌会员 Posts 1,744 Credits 4,103 From 甘肃.临泽
See if you need blank lines and lines starting with ;
Floor4 scriptor Posted 2007-02-26 09:57
银牌会员 Posts 555 Credits 1,187
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 ]
Floor5 vkill Posted 2007-02-26 11:00
金牌会员 Posts 1,744 Credits 4,103 From 甘肃.临泽
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.
Floor6 scriptor Posted 2007-02-26 11:23
银牌会员 Posts 555 Credits 1,187
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?
Floor7 slore Posted 2007-02-26 11:45
铂金会员 Posts 2,478 Credits 5,212
Floor8 scriptor Posted 2007-02-26 11:53
银牌会员 Posts 555 Credits 1,187
You mean using VBS or JS?

Of course that's possible. But what I want is bat.
Floor9 xycoordinate Posted 2007-02-26 23:07
中级用户 Posts 228 Credits 493 From 安徽
@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 ]
Floor10 qjbm Posted 2007-02-26 23:52
初级用户 Posts 44 Credits 125
Brother Scriptor,

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

Floor11 lxmxn Posted 2007-02-27 00:01
版主 Posts 4,938 Credits 11,386

It can handle blank lines and lines containing other characters:
Floor12 zh159 Posted 2007-02-27 00:02
金牌会员 Posts 1,467 Credits 3,687
The effect of floor 10 is the same as:

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 ]
Floor13 xycoordinate Posted 2007-05-05 10:47
中级用户 Posts 228 Credits 493 From 安徽
Originally posted by zh159 at 2007-2-27 00:02:
The effect of floor 10 is the same as:

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 ]
Floor14 hizebra Posted 2007-12-24 01:10
新手上路 Posts 5 Credits 19
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>
Floor15 ZJHJ Posted 2007-12-24 09:03
高级用户 Posts 374 Credits 609
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
1 2  Next
[ Contact the Union admin team - 中国DOS联盟 - Standard version ]
Sponsored by ifanr Inc | © 2001–2023