<p>How can I split long commands over multiple lines in a batch file?</p>

[{'answer_id': 69079, 'author': 'Wayne', 'author_id': 8236, 'author_profile': 'https://Stackoverflow.com/users/8236', 'pm_score': 11, 'selected': True, 'text': '<p>You can break up long lines with the caret <code>^</code> as long as you remember that the caret and the newline following it are completely removed. So, if there should be a space where you\'re breaking the line, include a space. <em>(<a href="https://stackoverflow.com/a/21000752/157247">More on that below.</a>)</em></p>\n\n<p>Example:</p>\n\n<pre><code>copy file1.txt file2.txt\n</code></pre>\n\n<p>would be written as:</p>\n\n<pre><code>copy file1.txt^\n file2.txt\n</code></pre>\n'}, {'answer_id': 4455750, 'author': 'jeb', 'author_id': 463115, 'author_profile': 'https://Stackoverflow.com/users/463115', 'pm_score': 8, 'selected': False, 'text': '<p>The rule for the caret is:</p>\n\n<p>A caret at the line end, appends the next line, the first character of the appended line will be escaped.</p>\n\n<p>You can use the caret multiple times, but the complete line must not exceed the maximum line length of ~8192 characters (Windows&nbsp;XP, Windows&nbsp;Vista, and Windows&nbsp;7).</p>\n\n<pre><code>echo Test1\necho one ^\ntwo ^\nthree ^\nfour^\n*\n--- Output ---\nTest1\none two three four*\n\necho Test2\necho one &amp; echo two\n--- Output ---\nTest2\none\ntwo\n\necho Test3\necho one &amp; ^\necho two\n--- Output ---\nTest3\none\ntwo\n\necho Test4\necho one ^\n&amp; echo two\n--- Output ---\nTest4\none &amp; echo two\n</code></pre>\n\n<p>To suppress the escaping of the next character you can use a redirection.</p>\n\n<p>The redirection has to be just before the caret.\nBut there exist one curiosity with redirection before the caret.</p>\n\n<p>If you place a token at the caret the token is removed.</p>\n\n<pre><code>echo Test5\necho one &lt;nul ^\n&amp; echo two\n--- Output ---\nTest5\none\ntwo\n\n\necho Test6\necho one &lt;nul ThisTokenIsLost^\n&amp; echo two\n--- Output ---\nTest6\none\ntwo\n</code></pre>\n\n<p>And it is also possible to <strong>embed line feeds</strong> into the string:</p>\n\n<pre><code>setlocal EnableDelayedExpansion\nset text=This creates ^\n\na line feed\necho Test7: %text%\necho Test8: !text!\n--- Output ---\nTest7: This creates\nTest8: This creates\na line feed\n</code></pre>\n\n<p>The empty line is important for the success. This works only with delayed expansion, else the rest of the line is ignored after the line feed.</p>\n\n<p>It works, because the caret at the line end ignores the next line feed and escapes the next character, even if the next character is also a line feed (carriage returns are always ignored in this phase).</p>\n'}, {'answer_id': 21000752, 'author': 'T.J. Crowder', 'author_id': 157247, 'author_profile': 'https://Stackoverflow.com/users/157247', 'pm_score': 7, 'selected': False, 'text': '<p><em>(This is basically a rewrite of <a href="https://stackoverflow.com/a/69079/157247">Wayne\'s answer</a> but with the confusion around the caret cleared up. So I\'ve posted it as a CW. I\'m not shy about editing answers, but <strong>completely rewriting them</strong> seems inappropriate.)</em></p>\n<p>You can break up long lines with the caret (<code>^</code>), just remember that the caret and the newline that follows it <em>are removed entirely</em> from the command, so if you put it where a space would be required (such as between parameters), be sure to include the space as well (either before the <code>^</code>, or at the beginning of the next line\xa0— that latter choice may help make it clearer it\'s a continuation).</p>\n<p>⚠ Note: The first character of the next line is escaped. So if it carries any special meaning (like <code>&amp;</code> or <code>|</code>), that meaning will be lost and it will be interpreted as a pure text character (see last example at bottom).</p>\n<p>Examples: <em>(all tested on Windows XP and Windows 7)</em></p>\n<pre><code>xcopy file1.txt file2.txt\n</code></pre>\n<p>can be written as:</p>\n<pre><code>xcopy^\n file1.txt^\n file2.txt\n</code></pre>\n<p>or</p>\n<pre><code>xcopy ^\nfile1.txt ^\nfile2.txt\n</code></pre>\n<p>or even</p>\n<pre><code>xc^\nopy ^\nfile1.txt ^\nfile2.txt\n</code></pre>\n<p>(That last works because there are no spaces betwen the <code>xc</code> and the <code>^</code>, and no spaces at the beginning of the next line. So when you remove the <code>^</code> and the newline, you get...<code>xcopy</code>.)</p>\n<p>For readability and sanity, it\'s probably best breaking only between parameters (be sure to include the space).</p>\n<p>Be sure that the <code>^</code> is <strong>not</strong> the last thing in a batch file, as there <a href="https://stackoverflow.com/questions/15466298/simple-caret-in-batch-file-consumes-all-memory">appears to be a major issue with that</a>.</p>\n<p>Here\'s an example of character escaped at the start of the next line:</p>\n<pre><code>xcopy file1.txt file2.txt ^\n&amp; echo copied successfully\n</code></pre>\n<p>This will not work because <code>&amp;</code> will be escaped and lose its special meaning, thus sending all of &quot;file1.txt file2.txt &amp; echo copied successfully&quot; as parameters to <code>xcopy</code>, causing an error (in this example).</p>\n<p>To circumvent, <strong>add a space</strong> at the beginning of the next line.</p>\n'}, {'answer_id': 25471056, 'author': 'Mohammed Safwat', 'author_id': 493119, 'author_profile': 'https://Stackoverflow.com/users/493119', 'pm_score': 4, 'selected': False, 'text': "<p>It seems however that splitting in the middle of the values of a for loop doesn't need a caret(and actually trying to use one will be considered a syntax error). For example,</p>\n\n<pre><code>for %n in (hello\nbye) do echo %n\n</code></pre>\n\n<p>Note that no space is even needed after hello or before bye.</p>\n"}, {'answer_id': 35371129, 'author': 'Todd Partridge', 'author_id': 4515565, 'author_profile': 'https://Stackoverflow.com/users/4515565', 'pm_score': 5, 'selected': False, 'text': '<p>Multiple commands can be put in parenthesis and spread over numerous lines; so something like <code>echo hi &amp;&amp; echo hello</code> can be put like this:</p>\n\n<pre><code>( echo hi\n  echo hello )\n</code></pre>\n\n<p>Also variables can help:</p>\n\n<pre><code>set AFILEPATH="C:\\SOME\\LONG\\PATH\\TO\\A\\FILE"\nif exist %AFILEPATH% (\n  start "" /b %AFILEPATH% -option C:\\PATH\\TO\\SETTING...\n) else (\n...\n</code></pre>\n\n<p>Also I noticed with carets (<code>^</code>) that the <code>if</code> conditionals liked them to follow only if a space was present:</p>\n\n<pre><code>if exist ^\n</code></pre>\n'}, {'answer_id': 64752041, 'author': 'npocmaka', 'author_id': 388389, 'author_profile': 'https://Stackoverflow.com/users/388389', 'pm_score': 3, 'selected': False, 'text': "<p>Though the carret will be preferable way to do this here's one more approach using macro that constructs a command by the passed arguments:</p>\n<pre><code>@echo off\n::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::\nset &quot;{{=setlocal enableDelayedExpansion&amp;for %%a in (&quot; &amp; set &quot;}}=&quot;::end::&quot; ) do if &quot;%%~a&quot; neq &quot;::end::&quot; (set command=!command! %%a) else (call !command! &amp; endlocal)&quot;\n:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::\n\n%{{%\n    echo\n    &quot;command&quot;\n    written\n    on a\n    few lines\n%}}%\n</code></pre>\n<p>command is easier to read without the carets but using special symbols e.g. brackets,redirection and so on will break it. So you can this for more simpler cases. Though you can still enclose parameters in double quotes</p>\n"}, {'answer_id': 68470236, 'author': 'Simon H', 'author_id': 7071220, 'author_profile': 'https://Stackoverflow.com/users/7071220', 'pm_score': 3, 'selected': False, 'text': "<p>One thing I did not find when searching for 'how to split a long DOS batch file line' was how to split something containing long quoted text.\nIn fact it IS covered in the answers above, but is not obvious.  Use Caret to escape them.\ne.g.</p>\n<pre><code>myprog &quot;needs this to be quoted&quot;\n</code></pre>\n<p>can be written as:</p>\n<pre><code>myprog ^&quot;needs this ^\nto be quoted^&quot;\n</code></pre>\n<p>but beware of starting a line with Caret after ending a line with caret - because it will come out as caret..?:</p>\n<pre><code>echo ^&quot;^\nneeds this ^\nto be quoted^\n^&quot;\n</code></pre>\n<p>-&gt; &quot;needs this to be quoted^&quot;</p>\n"}]
['https://Stackoverflow.com/questions/69068', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/-1/']