<?xml version="1.0" encoding="utf-8"?>
<feed xmlns="http://www.w3.org/2005/Atom">
	<title type="html"><![CDATA[Серый форум &mdash; CMD/BAT: Возможность создания heredoc]]></title>
	<link rel="self" href="https://forum.script-coding.com/extern.php?action=feed&amp;tid=10536&amp;type=atom" />
	<updated>2015-06-05T20:05:26Z</updated>
	<generator>PunBB</generator>
	<id>https://forum.script-coding.com/viewtopic.php?id=10536</id>
		<entry>
			<title type="html"><![CDATA[Re: CMD/BAT: Возможность создания heredoc]]></title>
			<link rel="alternate" href="https://forum.script-coding.com/viewtopic.php?pid=94373#p94373" />
			<content type="html"><![CDATA[<p>wisgest, согласен. Так стало лучше:<br /></p><div class="codebox"><pre><code>
@echo off

set &quot;hello=Hello world!&quot;
set &quot;lorem=Lorem ipsum dolor sit amet, consectetur adipiscing elit.&quot;

call :heredoc HTML &amp; goto :HTML
&lt;html&gt;
&lt;title&gt;!hello!&lt;/title&gt;
&lt;body&gt;
&lt;p&gt;Variables in heredoc should be surrounded by the exclamation mark (^!).&lt;/p&gt;
&lt;p&gt;!lorem!&lt;/p&gt;
&lt;p&gt;Exclamation mark (^!) and caret (^^) MUST be escaped with a caret (^^).&lt;/p&gt;
&lt;/body&gt;
&lt;/html&gt;
:HTML

goto :EOF

:: http://stackoverflow.com/a/15032476/3627676
:heredoc LABEL
setlocal enabledelayedexpansion
set go=
for /f &quot;delims=&quot; %%A in ( &#039;
    findstr /n &quot;^&quot; &quot;%~f0&quot;
&#039; ) do (
    set &quot;line=%%A&quot;
    set &quot;line=!line:*:=!&quot;

    if defined go (
        if /i &quot;!line!&quot; == &quot;!go!&quot; goto :EOF
        echo:!line!
    ) else (
        rem delims are ( ) &gt; &amp; | TAB , ; = SPACE
        for /f &quot;tokens=1-3 delims=()&gt;&amp;|    ,;= &quot; %%i in ( &quot;!line!&quot; ) do (
            if /i &quot;%%i %%j %%k&quot; == &quot;call :heredoc %1&quot; (
                set &quot;go=%%k&quot;
                if not &quot;!go:~0,1!&quot; == &quot;:&quot; set &quot;go=:!go!&quot;
            )
        )
    )
)
goto :EOF
</code></pre></div>]]></content>
			<author>
				<name><![CDATA[Rumata]]></name>
				<uri>https://forum.script-coding.com/profile.php?id=24846</uri>
			</author>
			<updated>2015-06-05T20:05:26Z</updated>
			<id>https://forum.script-coding.com/viewtopic.php?pid=94373#p94373</id>
		</entry>
		<entry>
			<title type="html"><![CDATA[Re: CMD/BAT: Возможность создания heredoc]]></title>
			<link rel="alternate" href="https://forum.script-coding.com/viewtopic.php?pid=94372#p94372" />
			<content type="html"><![CDATA[<div class="codebox"><pre><code>    for /f &quot;tokens=1-3 delims=()&gt;&amp;|    ,;= &quot; %%i in (&quot;!line!&quot;) do (
        if /i &quot;%%i %%j %%k&quot; == &quot;call :heredoc %1&quot; (
            set &quot;go=%1&quot;
            if not &quot;!go:~,1!&quot; == &quot;:&quot; set &quot;go=:!go!&quot;
        )
    )
</code></pre></div><p>1) из DELIMS исключено «<strong>:</strong>» — источник ложных срабатываний на команду закомментированную с помощью двоеточия или содержащую вызов не метки, а внешнего файла HEREDOC;<br />2) табуляция перенесена в DELIMS, после неё добавлены знаки «<strong>,;=</strong>», также воспринимаемые как разделители во внутренних командах;<br />3) добавлен ключ «/I».</p>]]></content>
			<author>
				<name><![CDATA[wisgest]]></name>
				<uri>https://forum.script-coding.com/profile.php?id=3850</uri>
			</author>
			<updated>2015-06-05T18:53:16Z</updated>
			<id>https://forum.script-coding.com/viewtopic.php?pid=94372#p94372</id>
		</entry>
		<entry>
			<title type="html"><![CDATA[Re: CMD/BAT: Возможность создания heredoc]]></title>
			<link rel="alternate" href="https://forum.script-coding.com/viewtopic.php?pid=94354#p94354" />
			<content type="html"><![CDATA[<p>Обновление:<br /></p><div class="codebox"><pre><code>
@echo off

set &quot;hello=Hello world!&quot;
set &quot;lorem=Lorem ipsum dolor sit amet, consectetur adipiscing elit.&quot;

call :heredoc :HTML &amp; goto :HTML
&lt;html&gt;
&lt;title&gt;!hello!&lt;/title&gt;
&lt;body&gt;
&lt;p&gt;Variables in heredoc should be surrounded by the exclamation mark (^!).&lt;/p&gt;
&lt;p&gt;!lorem!&lt;/p&gt;
&lt;p&gt;Exclamation mark (^!) and caret (^^) MUST be escaped with a caret (^^).&lt;/p&gt;
&lt;/body&gt;
&lt;/html&gt;
:HTML

goto :EOF

:: http://stackoverflow.com/a/15032476/3627676
:heredoc LABEL
setlocal enabledelayedexpansion
set go=
for /f &quot;delims=&quot; %%A in ( &#039;
    findstr /n &quot;^&quot; &quot;%~f0&quot;
&#039; ) do (
    set &quot;line=%%A&quot;
    set &quot;line=!line:*:=!&quot;

    if defined go (
        if &quot;!line!&quot; == &quot;!go!&quot; goto :EOF
        echo:!line!
    )

    rem Replace all TABs with SPACEs
    for /f &quot;tokens=1,2,3,4,5 delims=():&gt;&amp;| &quot; %%i in ( &quot;!line:    = !&quot; ) do (
        if &quot;%%i %%j :%%k&quot; == &quot;call heredoc %1&quot; set &quot;go=:%%k&quot;
        if &quot;%%i %%j :%%k&quot; == &quot;call heredoc :%1&quot; set &quot;go=:%%k&quot;
    )
)
goto :EOF
</code></pre></div><p>Упрощена проверка начала heredoc:<br />- важная команда теперь только call :heredoc LABEL, далее нет необходимости наличия goto LABEL, потому что с этого места можно перепрыгнуть на другу метку, конец сскрипта или вызвать команду exit /b<br />- удалил ненужную и лишнюю команду goto :next2<br />- можно писать как call :heredoc LABEL так и call :heredoc :LABEL</p>]]></content>
			<author>
				<name><![CDATA[Rumata]]></name>
				<uri>https://forum.script-coding.com/profile.php?id=24846</uri>
			</author>
			<updated>2015-06-04T08:10:34Z</updated>
			<id>https://forum.script-coding.com/viewtopic.php?pid=94354#p94354</id>
		</entry>
		<entry>
			<title type="html"><![CDATA[CMD/BAT: Возможность создания heredoc]]></title>
			<link rel="alternate" href="https://forum.script-coding.com/viewtopic.php?pid=92846#p92846" />
			<content type="html"><![CDATA[<p>Давно искал что-то подобное. В том смысле, что искал явно чужое и тыпался сделать что-то свое. В первом преуспел больше, а теперь пытаюсь компенсировать отсустствие второго некоторыми улучшениями первого. </p><p>Как говорит <a href="https://ru.wikipedia.org/wiki/Heredoc-синтаксис">википедия</a>:<br /></p><div class="quotebox"><blockquote><p>Heredóc-синтаксис — способ определения строковых переменных в исходном коде программ<br />. . . <br />Heredoc-синтаксис позволяет определить строку, не заключая её в кавычки, в связи с чем необходимость экранирования этих символов отпадает.</p></blockquote></div><p>Дословно, это возможность в тексте некоторой программы указать большой фрагмент произвольного текста внутри скрипта. Все более-менее продвинутые интерпретаторы имеют такие средства (за примерами предлагаю идти в гугл). А настоящем сообщении я поверду разговор о попытке внедрить heredoc в BAT/CMD файлы.</p><p><a href="http://stackoverflow.com/a/15032476/3627676">Исходные текст</a><br /><a href="http://stackoverflow.com/a/29329912/3627676">Улучшение</a> <br />-- перенаправление в файл в конце строки (пример закоммментирован)<br />-- допустимы любое количество пробельных символов-разделителей (пробел и табуляция)<br />-- более строгий синтаксис, но и в то же самое время более простой (при написании и чтении)</p><div class="codebox"><pre><code>
@echo off

set &quot;hello=Hello world!&quot;
set &quot;lorem=Lorem ipsum dolor sit amet, consectetur adipiscing elit.&quot;

rem ( call :heredoc HTML &amp; goto HTML ) &gt;outerfile.txt
call :heredoc HTML &amp; goto HTML
&lt;html&gt;
&lt;title&gt;!hello!&lt;/title&gt;
&lt;body&gt;
&lt;p&gt;Variables in heredoc should be surrounded by the exclamation mark (^!).&lt;/p&gt;
&lt;p&gt;!lorem!&lt;/p&gt;
&lt;p&gt;Exclamation mark (^!) and caret (^^) MUST be escaped with a caret (^^).&lt;/p&gt;
&lt;/body&gt;
&lt;/html&gt;
:HTML

goto :EOF

:: http://stackoverflow.com/a/15032476/3627676
:heredoc LABEL
setlocal enabledelayedexpansion
set go=
for /f &quot;delims=&quot; %%A in ( &#039;
    findstr /n &quot;^&quot; &quot;%~f0&quot;
&#039; ) do (
    set &quot;line=%%A&quot;
    set &quot;line=!line:*:=!&quot;

    if defined go (
        if &quot;!line!&quot; == &quot;!go!&quot; goto :EOF &amp; goto :next2
        echo:!line!
    )

    rem Replace all TABs with SPACEs
    for /f &quot;tokens=1,2,3,4,5 delims=():&gt;&amp; &quot; %%i in ( &quot;!line:    = !&quot; ) do (
        if &quot;%%i %%j %%k %%l&quot; == &quot;call heredoc %1 goto&quot; (
            set &quot;go=:%%m&quot;
        )
    )
)
goto :EOF
</code></pre></div>]]></content>
			<author>
				<name><![CDATA[Rumata]]></name>
				<uri>https://forum.script-coding.com/profile.php?id=24846</uri>
			</author>
			<updated>2015-03-29T15:29:59Z</updated>
			<id>https://forum.script-coding.com/viewtopic.php?pid=92846#p92846</id>
		</entry>
</feed>
