【发布时间】:2013-01-28 10:16:04
【问题描述】:
以下代码可以很好地打印段落
@echo off
setlocal disableDelayedExpansion
set "skip="
for /f "delims=:" %%N in (
'findstr /x /n ":::BeginText" "%~f0"'
) do if not defined skip set skip=%%N
>test.txt (
for /f "skip=%skip% tokens=*" %%A in (
'findstr /n "^" "%~f0"'
) do (
set "line=%%A"
setlocal enableDelayedExpansion
echo(!line:*:=!
endlocal
)
)
type test.txt
exit /b
:::BeginText
This text will be exactly preserved with the following limitations:
1) Each line will be terminated by CR LF even if original has only LF.
2) Lines are limited in length to approximately 8191 bytes.
Special characters like ^ & < > | etc. do not cause a problem.
Empty lines are preserved!
;Lines beginning with ; are preserved.
:::Leading : are preserved
有没有办法添加像:::Endtext 这样的文本标记,以便只有段落之间
:::BeginText 和 :::Endtext 被打印出来。
【问题讨论】:
-
这一行开头括号的作用是什么:
echo(!line:*:=!? -
@utapyngo:
!line:*:=!遵循以下语法:!varname:substr1=substr2!,因此varname中每次出现的substr1都将替换为substr2。在您的特定示例中,substr1包含一个掩码(*代表任意数量的任意字符),substr2为空,这意味着匹配的子字符串将被简单地删除。 -
@AndriyM:我知道。我在
echo之后询问左括号。我试图删除它,但没有它就无法工作。 -
@utapyngo:啊,是的,对不起,你说得很清楚,我忽略它是愚蠢的。这是为了防止例如当表达式计算为空字符串时,
ECHO is OFF消息。至于为什么(而不是别的什么,请看this answer。
标签: windows batch-file