如何使用批处理脚本计算文本文件中的行数并将值存储到变量中?
我想计算文本文件中的行数,然后必须将值存储到环境变量中。计算行数的命令是
findstr /R /N "^" file.txt | find /C ":"
我提到的问题如何使用bat脚本将命令表达式的结果存储在变量中? 然后我尝试了,
set cmd="findstr /R /N "^" file.txt | find /C ":" "
我收到错误消息,
FIND: Parameter format not correct
我怎样才能摆脱这个错误。
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,
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(17)
有一种比所有其他方法简单得多的方法。
显然,这是遗留下来的 MS-DOS 时代的产物。更多信息请参见:https://devblogs.microsoft.com/oldnewthing/20110825- 00/?p=9803
使用示例:
如果您的 Android 设备已连接到您的 PC 并且您的路径上有 Android SDK,则会打印出已安装的应用程序数量在您的设备上。
There is a much simpler way than all of these other methods.
Holdover from the legacy MS-DOS days, apparently. More info here: https://devblogs.microsoft.com/oldnewthing/20110825-00/?p=9803
Example use:
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.
您可以使用 FOR /F 循环将输出分配给变量。
我使用
cmd-variable
,因此无需转义 cmd-string 中的管道或其他字符,因为延迟扩展将“未更改”的字符串传递给 FOR 循环。You could use the FOR /F loop, to assign the output to a variable.
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.受到之前帖子的启发,
更短的方法:
行数
尝试一下。它在我的控制台中工作。
编辑:(
删除了“$”符号)
$ 将数字减少 1,因为它接受第一行作为字段名称,然后计算行数。
Inspired by the previous posts,
a shorter way of doing so:
The number of lines
Try it. It works in my console.
EDITED:
(the "$" sign removed)
$ reduces the number by 1 because it is accepting the first row as Field name and then counting the number of rows.
试试这个:
它消除了额外的 FindStr 并且不需要扩展。
- 已编辑以使用 ChrisJJ 的重定向建议。删除
TYPE
命令使其速度提高了三倍。Try this:
It eliminates the extra FindStr and doesn't need expansion.
- edited to use ChrisJJ's redirect suggestion. Removal of the
TYPE
command makes it three times faster.@Tony:您甚至可以摆脱
type %file%
命令。对于长文件,这应该更快。
@Tony: You can even get rid of the
type %file%
command.For long files this should be even quicker.
我通常使用类似这样的东西
for /f %%a in (%_file%) do (set /a Lines+=1)
I usually use something more like this
for /f %%a in (%_file%) do (set /a Lines+=1)
在批处理文件中,使用 %%A 而不是 %A
in a batch file, use %%A instead of %A
完美的解决方案是:
The perfect solution is:
我发现这个解决方案最适合创建一个自我维护的日志文件:
包括的环境变量是%clientname%远程客户端的计算机名%Date%是当前日期和%Time%当前时间。 :NEXT 在获取文件中的行数后调用。如果文件行数大于 %maxlines% 变量,它将转到 :EXITLOOP 并覆盖该文件,并使用第一行信息创建一个新文件。如果它小于 %maxlines% 变量,它只是将该行添加到当前文件中。
I found this solution to work best for creating a log file that maintains itself:
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.
下面的
:countLines
子例程接受两个参数:变量名;和一个文件名。统计文件中的行数,将结果存储在变量中,并将结果传回主程序。该代码具有以下功能:
(31^2)-1
。我知道它看起来很丑陋,但它涵盖了大多数边缘情况,而且速度快得惊人。
The
:countLines
subroutine below accepts two parameters: a variable name; and a filename. The number of lines in the file are counted, the result is stored in the variable, and the result is passed back to the main program.The code has the following features:
(31^2)-1
.I know it looks hideous, but it covers most edge-cases and is surprisingly fast.
您不需要使用查找。
这会迭代文件中的所有行,并将每行的计数器变量加 1。
You don't need to use find.
This iterates all lines in the file and increases the counter variable by 1 for each line.
只是:
字体:https://superuser.com/questions/959036/ wc-l 的 windows 等价物是什么
Just:
Font: https://superuser.com/questions/959036/what-is-the-windows-equivalent-of-wc-l
对于在 Windows 上安装了 git bash 的人来说,这是一个不错的惊喜:只需简单的旧 linux wc
-l
就可以在那里工作One nice surprise is for one who has
git bash
on his windows: just plain old linux wc-l <filename>
will works for you there在下面的代码中,变量名称是
SalaryCount
和TaxCount
现在,如果您需要将这些值输出到 csv 文件,您可以使用以下代码。
>
将覆盖文件的现有内容,>>
将把新数据附加到现有数据中。 CSV 将在 D:\CSVOutputPath 中生成In the below code, the variable name are
SalaryCount
andTaxCount
Now if you need to output these values to a csv file, you could use the below code.
The
>
will overwrite the existing content of the file and the>>
will append the new data to existing data. The CSV will be generated in D:\CSVOutputPath您可以将
type
的输出通过管道传送到for /f
循环的in(…)
子句中的find
:但是管道启动了一个子 shell,这会减慢速度。
或者,您可以将文件中的输入重定向到
find
中,如下所示:但是,如果文件以一个或多个空行结尾,则这种方法会给您一个比实际行数少 1 的答案,如所戏弄的由已故的 foxidrive 在 计算文件中的行数。
然后,您总是可以尝试:
问题是,上述命令的输出如下所示:
您可以拆分冒号上的字符串来获取计数,但如果文件名具有完整的冒号,则可能会有多个冒号小路。
这是我对这个问题的看法:
这将始终将计数存储在变量中。
最后一个小问题...
find
将空字符视为换行符。因此,如果偷偷摸摸的空值潜入您的文本文件,或者您想要计算 Unicode 文件中的行数,那么这个答案不适合您。You can pipe the output of
type
intofind
inside thein(…)
clause of afor /f
loop:But the pipe starts a subshell, which slows things down.
Or, you could redirect input from the file into
find
like so:But this approach will give you an answer 1 less than the actual number of lines if the file ends with one or more blank lines, as teased out by the late foxidrive in counting lines in a file.
And then again, you could always try:
The trouble is, the output from the above command looks like this:
You could split the string on the colon to get the count, but there might be more than one colon if the filename had a full path.
Here’s my take on that problem:
This will always store the count in the variable.
Just one last little problem…
find
treats null characters as newlines. So if sneaky nulls crept into your text file, or if you want to count the lines in a Unicode file, this answer isn’t for you.你也可以尝试
You can also try
还可以用通配符*进行标记,方便分组文件统计。
结果
---------- FR_OP133_OCCURENCES_COUNT_PER_DOCUMENTS_AVIFRS01_V1.TXT:2041
---------- FR_OP133_OCCURENCES_COUNT_PER_DOCUMENTS_AVIOST00_V1.TXT:315938
---------- FR_OP133_OCCURENCES_COUNT_PER_DOCUMENTS_AVIFRS00_V1 .TXT: 0
---- ------ FR_OP133_OCCURENCES_COUNT_PER_DOCUMENTS_CNTPTF00_V1.TXT:277
You can also mark with a wildcard symbol * to facilitate group files to count.
Result
---------- FR_OP133_OCCURENCES_COUNT_PER_DOCUMENTS_AVIFRS01_V1.TXT: 2041
---------- FR_OP133_OCCURENCES_COUNT_PER_DOCUMENTS_AVIOST00_V1.TXT: 315938
---------- FR_OP133_OCCURENCES_COUNT_PER_DOCUMENTS_AVIFRS00_V1.TXT: 0
---------- FR_OP133_OCCURENCES_COUNT_PER_DOCUMENTS_CNTPTF00_V1.TXT: 277