批处理脚本如何能做到的&QUOT相当于;猫<< EOF"? [英] How can a batch script do the equivalent of "cat << eof"?

查看:999
本文介绍了批处理脚本如何能做到的&QUOT相当于;猫<< EOF"?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Linux操作系统(击)有一个非常有用的功能对于像这样倾销文字文本到另一个文件:

 猫> see.txt<< EOF
内容进入
我的文件
EOF

我需要的是一个Windows批处理脚本相当。我还没有发现这样的内置功能,但我想我可以写一个子程序来做到这一点(我不想依靠任何不是在本地自Windows XP的),但我遇到麻烦。这是我迄今与<帮助href=\"http://stackoverflow.com/questions/7885437/batch-search-for-string-to-skip-lines-above-and-write-results-to-new-file\">various <一href=\"http://stackoverflow.com/questions/4310472/how-to-replace-a-string-on-the-second-line-in-a-text-file-using-a-batch-file\">sources:

 电话:catMyChunk myCustomText C:\\ see.txt
退出/ B转到:myCustomText
   这是我的测试文件
   希望你喜欢。    &LT;有这些&GT;
    %和这些%
    !这些也   是啊
:myCustomText:catMyChunk
    ::如果调用此函数2 ARGS,MYDELIM和outFile.txt
    ::哪里是要catted到outFile.txt
    ::和文本以&lt开始;线的制造&gt开始;转到:MYDELIM
    ::和处结束;线的制造&gt开始;:MYDELIM
    设置searchStart =转到:%〜1
    设置searchStop =:%〜1
    设置不过outFile =%〜2
    STARTLINE设置= 0
    设置底线= 0
    FOR / Fdelims =%%一个在('!FINDSTR -b -n searchStart%〜dpnx0')不要将STARTLINE = %%一个
    FOR / Fdelims =%%一个在('!FINDSTR -b -n searchStop%〜dpnx0')不设置底线= %%一个    集/ A linesLeftToRead =%底线% - %STARTLINE%
    德尔%不过outFile%
    如果%linesLeftToRead%LEQ0(
        回声错误的发现开始和结束delmieters为%searchStop%的catMyChunk常规
        出口/ B 1
    )
    SETLOCAL DisableDelayedExpansion
    FOR / F有usebackq跳过=%STARTLINE%delims =%%一中(`FINDSTR / N ^^%〜dpnx0')做(
        集ONELINE = %%一个
        SETLOCAL EnableDelayedExpansion
        集!ONELINE = ONELINE:*:!=
        集/ A linesLeftToRead- = 1
        如果!linesLeftToRead! LEQ 0退出/ B
        !回声(ONELINE&GT;!&GT;%不过outFile%
    )GOTO:EOF

所以,我的要求是,文本块是从字面上输出不受任何改变(即我不希望有逃脱的空行,%,!,&LT;等)。这code做几乎所有我需要的,但我还没有找到一种方法来正确地得到输出惊叹号。下面是输出我得到的,这是不完全正确的:

 这是我的测试文件
   希望你喜欢。    &LT;有这些&GT;
    %和这些%
     这些也   是啊

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

 :catMyChunk
    ::如果调用此函数2 ARGS,MYDELIM和outFile.txt
    ::哪里是要catted到outFile.txt
    ::和文本以&lt开始;线的制造&gt开始;转到:MYDELIM
    ::和处结束;线的制造&gt开始;:MYDELIM
    设置searchStart =转到:%〜1
    设置searchStop =:%〜1
    设置不过outFile =%〜2
    STARTLINE设置= 0
    设置底线= 0
    FOR / Fdelims =%%一个在('!FINDSTR -b -n searchStart%〜dpnx0')不要将STARTLINE = %%一个
    FOR / Fdelims =%%一个在('!FINDSTR -b -n searchStop%〜dpnx0')不设置底线= %%一个    集/ A linesLeftToRead =%底线% - %STARTLINE%
    德尔%不过outFile%
    如果%linesLeftToRead%LEQ0(
        回声错误的发现开始和结束delmieters为%searchStop%的catMyChunk常规
        出口/ B 1
    )
    SETLOCAL DisableDelayedExpansion
    FOR / F有usebackq跳过=%STARTLINE%delims =%%一中(`FINDSTR / N ^^%〜dpnx0')做(
        集ONELINE = %%一个
        集/ A linesLeftToRead- = 1
        SETLOCAL EnableDelayedExpansion
        集!ONELINE = ONELINE:*:!=
        如果!linesLeftToRead! LEQ 0退出/ B
        !回声(ONELINE&GT;!&GT;%不过outFile%
        ENDLOCAL
    )
    ENDLOCALGOTO:EOF


解决方案

在您的code缺少一个 ENDLOCAL 在for循环。结果
您可以为每个循环创建通过一个新的本地上下文中的 SETLOCAL EnableDelayedExpansion ,这将在文本文件中一些行爆炸。

和以preserve的感叹号(以及插入记号),你需要来回切换工艺
<一href=\"http://stackoverflow.com/questions/206114/dos-batch-files-how-to-read-a-file/4531090#4531090\">DOS批处理文件:如何读取文件

  SETLOCAL DisableDelayedExpansion
FOR / F有usebackq跳过=%STARTLINE%delims =%%一中(`FINDSTR / N ^^%〜dpnx0')做(
    集ONELINE = %%一个
    SETLOCAL EnableDelayedExpansion
    集!ONELINE = ONELINE:*:!=
    集/ A linesLeftToRead- = 1
    如果!linesLeftToRead! LEQ 0退出/ B
    !回声(ONELINE&GT;!&GT;%不过outFile%
    ** ** ENDLOCAL

In Linux (Bash) there is a very useful functionality for dumping literal text out to another file like this:

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

What I need is the equivalent for a Windows batch script. I haven't found this kind of functionality built-in, but I was thinking I could write a subroutine to do it (I don't want to rely on anything that isn't natively in Windows since XP), but I'm having trouble. Here's what I have so far with help from 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

So my requirement is that the chunk of text is output literally without any changes whatsoever (i.e. I don't want to have to escape blank lines, %, !, <, etc.). This code is doing almost everything I need, except I haven't found a way to get the exclamation points output properly. Here is the output I get, which isn't quite right:

   This is my test file
   Hope you like it.

    <got these>
    % and these %
     these too

   yeah

Edit: For anyone wanting the modified version of the subroutine that now works, here it is:

: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

解决方案

In your code is missing a single endlocal in your FOR-loop.
You create for each loop a new local-context through the setlocal EnableDelayedExpansion, this will explode with some more lines in your text file.

And to preserve the exclamation marks (and also the carets) you need the toggling technic 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**
)

这篇关于批处理脚本如何能做到的&QUOT相当于;猫&LT;&LT; EOF&QUOT;?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆