2016-08-29 53 views

回答

2

只循環findstr結果,搜索至少一個字符(正則表達式=。)行和計數。 空行不匹配。

@echo off 

set counter=0 
for /F %%a in ('findstr /R . test.txt') do (set /A counter=%counter%+1) 

echo %counter% 

如果你考慮到只包含空格線是空白的,只包含空格過濾掉線也由/C:"[^ ]"

findstr /R /C:"[^ ]" test.txt 
0
@echo off 
:==== 
set "file=c:\file.txt" 
:===== 
set count=1 
for /f "usebackq" %a in ("%file%") do set /a count=count+1 
echo %count% 

雖然包含;=,這將線替換.<tab>,<space>,<new line>符號(不僅空格和製表符和新行)

1

只需通過(在該示例file.txt)的文本文件內容使用for /F循環,因爲它跳過空行,並使用計數器變量迭代:

set /A "COUNT=0" 
for /F useback^ delims^=^ eol^= %%L in ("file.txt") do set /A "COUNT+=1" 
echo %COUNT% 

奇數加引號選項字符串語法useback^ delims^=^ eol^=,以便用來將eoldelims都設置爲空(請注意,eol默認爲;)。如果您將只包含空格(空格,製表符)的行視爲空白,只需刪除選項delims^=^

3
findstr "." test.txt | find /c /v "" 

findstr將使用針對行中的正則表達式.(匹配至少一個字符)的輸入文件,並且將管中的匹配線find到輸出線計數。