Batch file 在批处理文件中使用变量作为标志
我正在编写一个批处理文件,该批处理文件通过文本文件进行解析,并且仅当它读取所述文件中的特定行时才生成输出。考虑下面的文本文件名为Simult.txt: 假设我的批处理文件的目标是输出单词“Dog”在每个列表中出现的次数。因此在sample.txt中有两个列表:一个有1个“Dog”实例,另一个有2个。为了在批处理文件中实现这一点,我编写了以下代码:Batch file 在批处理文件中使用变量作为标志,batch-file,dos,Batch File,Dos,我正在编写一个批处理文件,该批处理文件通过文本文件进行解析,并且仅当它读取所述文件中的特定行时才生成输出。考虑下面的文本文件名为Simult.txt: 假设我的批处理文件的目标是输出单词“Dog”在每个列表中出现的次数。因此在sample.txt中有两个列表:一个有1个“Dog”实例,另一个有2个。为了在批处理文件中实现这一点,我编写了以下代码: @echo off set count=0 set first_iteration=1 for /f "tokens=*" %%A in (samp
@echo off
set count=0
set first_iteration=1
for /f "tokens=*" %%A in (sample.txt) do (
echo %%A > tempfile.txt
FINDSTR /R ".*List.*" tempfile.txt > NUL
REM If true, this is a list header
if errorlevel 1 (
if %first_iteration% EQU 1 (
REM PROBLEM HAPPENS HERE
set first_iteration=0
) else (
echo %count% >> log.txt
set count=0
)
REM An animal has been found
) else (
FINDSTR /R ".Dog.*" tempfile.txt > NUL
if NOT errorlevel 1 (
set \A count+=1
)
)
)
del tempfile.txt
echo %count% >> log.txt
pause
因此,基本上这段代码的工作原理是,我必须打印出在它读取新的列表头(List1和List2)时找到了多少“狗”。第一次发生这种情况时,计数将为零,因为它也是sample.txt文件的第一行。例如,读取List1后,它会一直读取,直到找到List2为止,计数为1。然后计数重置为0,因为它正在读取新列表。当批处理文件读取第二个列表时,因为它也是最终列表,所以它需要输出它拥有的任何计数
*first_iteration*变量跟踪批处理文件是否正在读取第一个列表,以便知道何时不输出计数。但问题是,*first_iteration*的值不会因为批处理如何在单行命令中解释变量值而改变(如果仔细观察,所有if/else语句都包含在一组括号中)
那么,有没有办法以批处理文件的形式实现我的目标呢
假设我的批处理文件的目标是输出
在每个列表中出现单词“Dog”的次数
没有测试。。。不记得你是否应该使用!或者在循环后调用计数器时使用%。您可以使用
!第一次迭代代码>用于延迟评估。在使用SETLOCAL ENABLEDELAYEDEXPANSION
启用它之后,您需要在脚本中的多个位置使用它
@echo off
SETLOCAL ENABLEDELAYEDEXPANSION
set count=0
set first_iteration=1
for /f "tokens=*" %%A in (sample.txt) do (
echo %%A > tempfile.txt
FINDSTR /R ".*List.*" tempfile.txt > NUL
REM If true, this is a list header
REM your post was missing the NOT here
if NOT errorlevel 1 (
if !first_iteration! EQU 1 (
SET first_iteration=0
) else (
echo !count! >> log.txt
set count=0
)
REM An animal has been found
) else (
REM your post was missing the first * on this line
FINDSTR /R ".*Dog.*" tempfile.txt > NUL
if NOT errorlevel 1 (
REM your post used a \ instead of a / here
set /A count=1+!count!
)
)
)
del tempfile.txt
echo %count% >> log.txt
工作得很有魅力!谢谢!
SET counter=0
SETLOCAL ENABLEDELAYEDEXPANSION
FOR /F "USEBACKQ tokens=*" %%F IN (`TYPE "C:\Folder\sample.txt" ^| FIND /I "dog"`) DO (
SET /a counter=!counter!+1
)
ECHO Dog shows up !counter! many times in this file.
@echo off
SETLOCAL ENABLEDELAYEDEXPANSION
set count=0
set first_iteration=1
for /f "tokens=*" %%A in (sample.txt) do (
echo %%A > tempfile.txt
FINDSTR /R ".*List.*" tempfile.txt > NUL
REM If true, this is a list header
REM your post was missing the NOT here
if NOT errorlevel 1 (
if !first_iteration! EQU 1 (
SET first_iteration=0
) else (
echo !count! >> log.txt
set count=0
)
REM An animal has been found
) else (
REM your post was missing the first * on this line
FINDSTR /R ".*Dog.*" tempfile.txt > NUL
if NOT errorlevel 1 (
REM your post used a \ instead of a / here
set /A count=1+!count!
)
)
)
del tempfile.txt
echo %count% >> log.txt