Batch file DOS FOR循环中是否可以有多个EOL字符?
有人知道在DOS批处理脚本中使用多个字符作为“eol”字符的方法吗。我想同时使用“;”和“#”作为注释行的字符Batch file DOS FOR循环中是否可以有多个EOL字符?,batch-file,for-loop,Batch File,For Loop,有人知道在DOS批处理脚本中使用多个字符作为“eol”字符的方法吗。我想同时使用“;”和“#”作为注释行的字符 type t.txt this is a line #this is a commented line ;is this a commented line last line for /f "usebackq tokens=* eol=; eol=#" %f in (`type t.txt`) do (echo "%f") "this is a line" ";is this a c
type t.txt
this is a line
#this is a commented line
;is this a commented line
last line
for /f "usebackq tokens=* eol=; eol=#" %f in (`type t.txt`) do (echo "%f")
"this is a line"
";is this a commented line"
"last line"
for /f "usebackq tokens=* eol=;#" %f in (`type t.txt`) do (echo "%f")
#" was unexpected at this time.
可以在内部嵌套for循环,以定义另一个eol:
for /f "usebackq eol=; delims=" %%f in (`type t.txt`) do (
for /f "eol=# delims=" %%Z in ("%%~f") do echo %%Z
)
但作为命令行,它可能是一个很长的表达式
令我惊讶的是,cmd.exe
并没有抱怨第一个示例中的两个eol定义,尽管它只使用了第一个定义。另一种方法是使用FINDSTR
过滤结果:
for /f "usebackq delims=" %%f in (`type t.txt^| findstr /i /b /v "; #"`) do ( echo %%f )
或从命令行:
for /f "usebackq delims=" %f in (`type type.txt^| findstr /i /b /v "; #"`) do ( echo %f )
对于循环,如果
内的IF条件很少,也可能出现这种情况,但这将需要延迟扩展,并且不便于在命令行中使用。仅此命令即可解决此问题,如下所述:
findstr /V /B "# ;" t.txt