windows 如何计算文本文件中的行数并使用批处理脚本将值存储到变量中?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5664761/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 08:16:52  来源:igfitidea点击:

How to count no of lines in text file and store the value into a variable using batch script?

windowsbatch-filecmd

提问by rashok

I want to count the no of lines in a text file and then the value has to be stored into a environment variable. The command to count the no of lines is

我想计算文本文件中的行数,然后必须将值存储到环境变量中。计算行数的命令是

findstr /R /N "^" file.txt | find /C ":"

I refered the question How to store the result of a command expression in a variable using bat scripts?Then I tried,

我参考了问题如何使用 bat 脚本将命令表达式的结果存储在变量中?然后我试了一下,

set cmd="findstr /R /N "^" file.txt | find /C ":" "

I am getting the error message,

我收到错误消息,

FIND: Parameter format not correct

How could i get rid of this error.

我怎么能摆脱这个错误。

采纳答案by jeb

You could use the FOR /F loop, to assign the output to a variable.

您可以使用 FOR /F 循环将输出分配给变量。

I use the cmd-variable, so it's not neccessary to escape the pipe or other characters in the cmd-string, as the delayed expansion passes the string "unchanged" to the FOR-Loop.

我使用cmd-variable, 因此没有必要转义 cmd 字符串中的管道或其他字符,因为延迟扩展会将字符串“未更改”传递给 FOR 循环。

@echo off
cls
setlocal EnableDelayedExpansion
set "cmd=findstr /R /N "^^" file.txt | find /C ":""

for /f %%a in ('!cmd!') do set number=%%a
echo %number%

回答by user169771

There is a much simpler way than all of these other methods.

有一种比所有其他方法更简单的方法。

find /v /c "" filename.ext

Holdover from the legacy MS-DOS days, apparently. More info here: https://devblogs.microsoft.com/oldnewthing/20110825-00/?p=9803

显然,遗留下来的 MS-DOS 时代。更多信息:https: //devblogs.microsoft.com/oldnewthing/20110825-00/?p=9803

Example use:

使用示例:

adb shell pm list packages | find /v /c ""

If your android device is connected to your PC and you have the android SDK on your path, this prints out the number of apps installed on your device.

如果您的 android 设备已连接到您的 PC 并且您的路径上有 android SDK,这会打印出您设备上安装的应用程序数量。

回答by Tin Amaranth

Inspired by the previous posts, a shorter way of doing so:

受之前帖子的启发,一种更短的方法:

CMD.exe
C:\>FINDSTR /R /N "^.*$" file.txt | FIND /C ":"

The number of lines

行数

Try it. It works in my console.

尝试一下。它在我的控制台中工作。

EDITED:

编辑:

(the "$" sign removed)

(删除了“$”符号)

FINDSTR /R /N "^.*" file.txt | FIND /C ":"

$ reduces the number by 1 because it is accepting the first row as Field name and then counting the number of rows.

$ 将数字减少 1,因为它接受第一行作为字段名称,然后计算行数。

回答by Tony

Try this:

尝试这个:

@Echo off
Set _File=file.txt
Set /a _Lines=0
For /f %%j in ('Find "" /v /c ^< %_File%') Do Set /a _Lines=%%j
Echo %_File% has %_Lines% lines.

It eliminates the extra FindStr and doesn't need expansion.

它消除了额外的 FindStr 并且不需要扩展。



- edited to use ChrisJJ's redirect suggestion. Removal of the TYPEcommand makes it three times faster.

- 编辑以使用 ChrisJJ 的重定向建议。删除该TYPE命令使其速度提高三倍。

回答by Dazzam

@Tony: You can even get rid of the type %file%command.

@Tony:您甚至可以摆脱该type %file%命令。

for /f "tokens=2 delims=:" %%a in ('find /c /v "" %_file%') do set /a _Lines=%%a

For long files this should be even quicker.

对于长文件,这应该更快。

回答by Ray

I usually use something more like this for /f %%a in (%_file%) do (set /a Lines+=1)

我通常使用更像这样的东西 for /f %%a in (%_file%) do (set /a Lines+=1)

回答by Andreas M?ngs

You don't need to use find.

您不需要使用查找。

@echo off
set /a counter=0
for /f %%a in (filename) do set /a counter+=1
echo Number of lines: %counter%

This iterates all lines in the file and increases the counter variable by 1 for each line.

这将迭代文件中的所有行,并为每一行将计数器变量增加 1。

回答by Tom Warfield

for /f "usebackq" %A in (`TYPE c:\temp\file.txt ^| find /v /c "" `) do set numlines=%A

in a batch file, use %%A instead of %A

在批处理文件中,使用 %%A 而不是 %A

回答by joopykunk

I found this solution to work best for creating a log file that maintains itself:

我发现这个解决方案最适合创建一个自我维护的日志文件:

setlocal enabledelayedexpansion
SET /A maxlines= 10
set "cmd=findstr /R /N "^^" "filename.txt" | find /C ":""
 for /f %%a in ('!cmd!') do set linecount=%%a
GOTO NEXT

:NEXT 
 FOR /F %%A IN ("filename.txt") DO ( 
  IF %linecount% GEQ %maxlines% GOTO ExitLoop
  echo %clientname% %Date% %Time% >> "filename.txt")
 EXIT

:ExitLoop
 echo %clientname% %Date% %Time% > "filename.txt"
 EXIT

Environmental variables included are %clientname% the computername of the remote client %Date% is the current date and %Time% the current time. :NEXT is called after getting the number of lines in the file. If the file line count is greater than the %maxlines% variable it goes to the :EXITLOOP where it overwrites the file, creating a new one with the first line of information. if it is less than the %maxlines% variable it simply adds the line to the current file.

包括的环境变量是 %clientname% 远程客户端的计算机名 %Date% 是当前日期和 %Time% 当前时间。:NEXT 在获取文件中的行数后调用。如果文件行数大于 %maxlines% 变量,它会转到 :EXITLOOP 覆盖文件,用第一行信息创建一个新文件。如果它小于 %maxlines% 变量,它只会将该行添加到当前文件中。

回答by Carlos.Candido