see.txt % and these %-6ren">
gpt4 book ai didi

batch-file - 批处理脚本如何做相当于 "cat << eof"的工作?

转载 作者:行者123 更新时间:2023-12-01 01:23:49 24 4
gpt4 key购买 nike

在 Linux (Bash) 中有一个 very useful functionality用于将文字文本转储到另一个文件中,如下所示:

cat > see.txt << EOF
contents going into
my file
EOF

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

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

<got these>
% and these %
these too

yeah

编辑:
对于任何想要现在工作的子程序的修改版本的人来说,这里是:
: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

最佳答案

您的代码缺少一个 endlocal在你的 FOR 循环中。
然后,您将通过 setlocal EnableDelayedExpansion 为每个循环创建一个新的本地上下文。 ,这将在您的文本文件中添加更多行。
此外,为了保留感叹号(以及插入符号),您需要切换技术:
DOS batch files: How to read a file?

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
)

关于batch-file - 批处理脚本如何做相当于 "cat << eof"的工作?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7954719/

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