循环文本文件中的所有行,同时保留空行和所有字符

问题描述 投票:0回答:1

我需要循环遍历文本文件中的每一行,但仍然有空行。我能找到的常见解决方案如下:

for /f "tokens=1* delims=:" %%i in ('findstr /n "^" "%textfile%"') do ( call :IsLabel _return "%%b" )

这将为每一行添加行号和冒号字符作为前缀,例如:

21:R 30 35 89jtj3 G)(#G_ 23ty9ug9dg

问题是,如果第一个字符是冒号 (

:
),它会因为
delims=:
循环中的
for
而被丢弃。

所以

30::mylabel
就变成了
mylabel
而不是
:mylabel

有没有办法让

findstr
使用冒号以外的其他字符?

windows for-loop batch-file delimiter findstr
1个回答
0
投票

基于@Compo的评论参考

for /f "tokens=1* delims=:" %%i in ('findstr /n "^" "%textfile%"') do ( 
  set _buffer=%%b
  setlocal enabledelayedexpansion
  set _buffer=!_buffer:*:=!
  call :IsLabel _return _buffer && ( action if IsLabel returns 0 ) || action if IsLabel returns nonzero )
  endlocal 
)
© www.soinside.com 2019 - 2024. All rights reserved.