4

次のコードは、段落を印刷するのに非常にうまく機能します

@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

4

1 に答える 1

8

もちろん :-)

また、スクリプト内に複数の名前付き段落を埋め込み、それぞれに固有のラベルを使用して選択的に書き込むことができます。

名前付きテキストは、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
于 2013-01-28T12:37:46.403 に答える