批处理脚本如何执行等效于“cat << eof”的操作?

Jar*_*red 7 batch-file

在 Linux (Bash) 中,有一个非常有用的功能可以将文字文本转储到另一个文件中,如下所示:

cat > see.txt << EOF
contents going into
my file
EOF
Run Code Online (Sandbox Code Playgroud)

我需要的是 Windows 批处理脚本的等效项。我还没有找到内置的这种功能,但我想我可以编写一个子程序来做到这一点(我不想依赖自 XP 以来 Windows 中没有的任何东西),但我遇到麻烦。到目前为止,这是我在各种 来源的帮助下所获得的:

call:catMyChunk myCustomText c:\see.txt
exit /b

goto:myCustomText
   This is my test file
   Hope you like it.

    <got these>
    % and these %
    ! these too

   yeah
:myCustomText

:catMyChunk
    ::Should call this function with 2 args, MYDELIM and outFile.txt
    ::where is to be catted to outFile.txt
    ::and text starts with <beginning of line>goto:MYDELIM
    ::and ends with <beginning of line>:MYDELIM
    set searchStart=goto:%~1
    set searchStop=:%~1
    set outFile=%~2
    set startLine=0
    set endLine=0
    for /f "delims=:" %%a in ('findstr -b -n !searchStart! %~dpnx0') do set "startLine=%%a"
    for /f "delims=:" %%a in ('findstr -b -n !searchStop!  %~dpnx0') do set "endLine=%%a"

    set /a linesLeftToRead=%endLine% - %startLine%
    del %outFile%
    if "%linesLeftToRead%" LEQ "0" (
        echo Error finding start and end delmieters for %searchStop% in catMyChunk routine
        exit /B 1
    )
    setlocal DisableDelayedExpansion
    for /f "usebackq skip=%startLine% delims=" %%a in (`"findstr /n ^^ %~dpnx0"`) do (
        set "oneLine=%%a"
        setlocal EnableDelayedExpansion
        set "oneLine=!oneLine:*:=!"
        set /a linesLeftToRead-=1
        if !linesLeftToRead! LEQ 0 exit /B
        echo(!oneLine!>>%outFile%
    )

goto: EOF
Run Code Online (Sandbox Code Playgroud)

所以我的要求是文本块在没有任何更改的情况下按字面输出(即我不想转义空行、%、!、< 等)。这段代码几乎完成了我需要的一切,除了我还没有找到正确输出感叹号的方法。这是我得到的输出,这不太正确:

   This is my test file
   Hope you like it.

    <got these>
    % and these %
     these too

   yeah
Run Code Online (Sandbox Code Playgroud)

编辑:对于任何想要现在工作的子程序的修改版本的人,这里是:

:catMyChunk
    ::Should call this function with 2 args, MYDELIM and outFile.txt
    ::where is to be catted to outFile.txt
    ::and text starts with <beginning of line>goto:MYDELIM
    ::and ends with <beginning of line>:MYDELIM
    set searchStart=goto:%~1
    set searchStop=:%~1
    set outFile=%~2
    set startLine=0
    set endLine=0
    for /f "delims=:" %%a in ('findstr -b -n !searchStart! %~dpnx0') do set "startLine=%%a"
    for /f "delims=:" %%a in ('findstr -b -n !searchStop!  %~dpnx0') do set "endLine=%%a"

    set /a linesLeftToRead=%endLine% - %startLine%
    del %outFile%
    if "%linesLeftToRead%" LEQ "0" (
        echo Error finding start and end delmieters for %searchStop% in catMyChunk routine
        exit /B 1
    )
    setlocal DisableDelayedExpansion
    for /f "usebackq skip=%startLine% delims=" %%a in (`"findstr /n ^^ %~dpnx0"`) do (
        set "oneLine=%%a"
        set /a linesLeftToRead-=1
        setlocal EnableDelayedExpansion
        set "oneLine=!oneLine:*:=!"
        if !linesLeftToRead! LEQ 0 exit /B
        echo(!oneLine!>>%outFile%
        endlocal
    )
    endlocal

goto: EOF
Run Code Online (Sandbox Code Playgroud)

jeb*_*jeb 4

endlocal您的代码在 FOR 循环中缺少一个。

然后,您将通过 为每个循环创建一个新的本地上下文setlocal EnableDelayedExpansion,这将在文本文件中产生更多行。

另外,要保留感叹号(以及插入符号),您需要切换技术: DOS 批处理文件:如何读取文件?

setlocal DisableDelayedExpansion
for /f "usebackq skip=%startLine% delims=" %%a in (`"findstr /n ^^ %~dpnx0"`) do (
    set "oneLine=%%a"
    setlocal EnableDelayedExpansion
    set "oneLine=!oneLine:*:=!"
    set /a linesLeftToRead-=1
    if !linesLeftToRead! LEQ 0 exit /B
    echo(!oneLine!>>%outFile%
    endlocal
)
Run Code Online (Sandbox Code Playgroud)