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-14 23:27
中国DOS联盟论坛 » DOS批处理 & 脚本技术(批处理室) » Ask: How to count the number of lines of strings in a text View 1,409 Replies 5
Original Poster Posted 2007-03-22 08:23 ·  中国 河南 新乡 联通
初级用户
Credits 58
Posts 27
Joined 2007-03-21 06:36
19-year member
UID 82427
Gender Male
Status Offline
Count the number of strings in any line of the text. For example, if a line has "sdgdsg sdgdsg sdgfdgh", you can know there are 3 strings.
Floor 2 Posted 2007-03-22 12:23 ·  中国 湖北 武汉 电信
版主
★★★★★
Credits 11,386
Posts 4,938
Joined 2006-07-23 17:10
19-year member
UID 59080
Status Offline
Run the following batch script. Enter the file name to be checked, and after pressing Enter, the number of words per line will be displayed. Words are strings separated by spaces or horizontal tabs.

@Echo Off&SetLocal EnableDelayedExpansion

set/p file=Please input the file you want to check:
if not defined file (echo Error&pause&exit/b)
if not exist "%file%" (echo Error&pause&exit/b)
for /f "delims=: tokens=1*" %%a in ('findstr /n .* "%file%"') do (
set b=&set a=%%b
if not "!a!"=="" (
for %%h in (!a!) do set /a b+=1
echo/Line %%a:!b! words
) else (echo/Line %%a:0 words)
)
pause
Floor 3 Posted 2007-03-22 20:42 ·  中国 河南 新乡 联通
初级用户
Credits 58
Posts 27
Joined 2007-03-21 06:36
19-year member
UID 82427
Gender Male
Status Offline
Thanks to the administrator for providing
Floor 4 Posted 2007-03-22 20:56 ·  中国 河南 新乡 联通
初级用户
Credits 58
Posts 27
Joined 2007-03-21 06:36
19-year member
UID 82427
Gender Male
Status Offline
Continuing to ask: What does!a! mean and how to use it?
Floor 5 Posted 2007-03-22 21:22 ·  中国 湖北 武汉 电信
版主
★★★★★
Credits 11,386
Posts 4,938
Joined 2006-07-23 17:10
19-year member
UID 59080
Status Offline
  Re bza2006:

!a! is equivalent to %a%, which means referencing the value of variable a. But it needs to use variable delay (setlocal enabledelayedexpansion) in for, so use! instead of % to reference its value.

You can take a look at the following related explanations:

http://www.cn-dos.net/forum/viewthread.php?tid=28273&fpage=1&highlight=%E5%8F%98%E9%87%8F%E5%BB%B6%E8%BF%9F

http://www.cn-dos.net/forum/viewthread.php?tid=20733&fpage=1&highlight=%E5%8F%98%E9%87%8F%E5%BB%B6%E8%BF%9F
Floor 6 Posted 2007-03-23 08:14 ·  中国 河南 新乡 联通
初级用户
Credits 58
Posts 27
Joined 2007-03-21 06:36
19-year member
UID 82427
Gender Male
Status Offline
Thanks for the guidance, and I'm studying hard~
Forum Jump: