如何算没有台词的文本文件,并使用批处理脚本值存储到一个变量? [英] How to count no of lines in text file and store the value into a variable using batch script?
问题描述
我要算没有台词的文本文件,然后将值必须存储到一个环境变量。该命令计算行没有为
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 ":"
我所指向的问题如何存储使用蝙蝠脚本变量的命令前pression的结果?
然后我尝试,
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.
推荐答案
您可以用FOR / F回路,分配输出到一个变量。
You could use the FOR /F loop, to assign the output to a variable.
我用 CMD-变量
,所以它不是neccessary逃避在cmd串管道或其他字符,如延迟扩展将字符串不变到for循环。
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.
@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%
这篇关于如何算没有台词的文本文件,并使用批处理脚本值存储到一个变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!