gpt4 book ai didi

windows - 在windows批处理中打印一段

转载 作者:可可西里 更新时间:2023-11-01 13:53:44 25 4
gpt4 key购买 nike

下面的代码打印段落效果很好

@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 被打印出来。

最佳答案

当然 :-)

您可以在脚本中嵌入多个命名段落,并通过为每个段落使用唯一标签来选择性地编写它们。

命名文本可以出现在脚本中的任何位置,只要 GOTO 和/或 EXIT/B 阻止文本被执行即可。

为方便起见,下面的脚本将逻辑封装在 :printParagraph 例程中。

@echo off
setlocal disableDelayedExpansion
goto :start

:::BeginText1
Paragraph 1
is preserved

Bye!
:::EndText

:start
echo Print paragraph 1 directly to screen
echo ------------------------------------------
call :printParagraph 1
echo ------------------------------------------
echo(
echo(

call :printParagraph 2 >test.txt
echo Write paragraph 2 to a file and type file
echo ------------------------------------------
type test.txt
echo ------------------------------------------
echo(
echo(

echo Print paragraph 3 directly to screen
echo ------------------------------------------
call :printParagraph 3
echo ------------------------------------------
echo(
echo(

exit /b

:::BeginText2
This is paragraph 2

Pure poetry
:::EndText

:printParagraph
set "skip="
for /f "delims=:" %%N in (
'findstr /x /n ":::BeginText%~1" "%~f0"'
) do if not defined skip set skip=%%N
set "end="
for /f "delims=:" %%N in (
'findstr /x /n ":::EndText" "%~f0"'
) do if %%N gtr %skip% if not defined end set end=%%N
for /f "skip=%skip% tokens=*" %%A in (
'findstr /n "^" "%~f0"'
) do (
for /f "delims=:" %%N in ("%%A") do if %%N geq %end% exit /b
set "line=%%A"
setlocal enableDelayedExpansion
echo(!line:*:=!
endlocal
)
exit /b

:::BeginText3
One more...

...for good measure
:::EndText

关于windows - 在windows批处理中打印一段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14559789/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com