<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="http://blogs.msdn.com/utility/FeedStylesheets/rss.xsl" media="screen"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:wfw="http://wellformedweb.org/CommentAPI/"><channel><title>Exiting a batch file without exiting the command shell -and- batch file subroutines</title><link>http://blogs.msdn.com/b/oldnewthing/archive/2012/08/02/10334559.aspx</link><description>Goto considered useful, and call me, maybe.</description><dc:language>en-US</dc:language><generator>Telligent Evolution Platform Developer Build (Build: 5.6.50428.7875)</generator><item><title>re: Exiting a batch file without exiting the command shell -and- batch file subroutines</title><link>http://blogs.msdn.com/b/oldnewthing/archive/2012/08/02/10334559.aspx#10336618</link><pubDate>Fri, 03 Aug 2012 15:34:41 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:10336618</guid><dc:creator>skSdnW</dc:creator><description>&lt;p&gt;@Kenn:&lt;/p&gt;
&lt;p&gt;This forces you to know when you are executing a batchfile, not really nice when you have a batch implementation of which etc&lt;/p&gt;
&lt;div class="post"&gt;[&lt;i&gt;You can play it safe and put &lt;code&gt;call&lt;/code&gt; in front of everything. -Raymond&lt;/i&gt;]&lt;/div&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=10336618" width="1" height="1"&gt;</description></item><item><title>re: Exiting a batch file without exiting the command shell -and- batch file subroutines</title><link>http://blogs.msdn.com/b/oldnewthing/archive/2012/08/02/10334559.aspx#10336522</link><pubDate>Fri, 03 Aug 2012 10:23:43 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:10336522</guid><dc:creator>Kenn</dc:creator><description>&lt;p&gt;@WndSys&lt;/p&gt;
&lt;p&gt;You can work around this by always using CALL to run a batch file. &amp;nbsp;&lt;/p&gt;
&lt;p&gt;Let&amp;#39;s say script.bat exits with EXIT /B 0 on success and EXIT /B 1 on error. &amp;nbsp;The following is not reliable when run from an interactive command prompt:&lt;/p&gt;
&lt;p&gt;script.bat &amp;amp;&amp;amp; echo OK&lt;/p&gt;
&lt;p&gt;script.bat || echo failed&lt;/p&gt;
&lt;p&gt;However, this works:&lt;/p&gt;
&lt;p&gt;call script.bat &amp;amp;&amp;amp; echo OK&lt;/p&gt;
&lt;p&gt;call script.bat || echo failed&lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=10336522" width="1" height="1"&gt;</description></item><item><title>re: Exiting a batch file without exiting the command shell -and- batch file subroutines</title><link>http://blogs.msdn.com/b/oldnewthing/archive/2012/08/02/10334559.aspx#10336396</link><pubDate>Fri, 03 Aug 2012 01:11:08 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:10336396</guid><dc:creator>DavidPLB</dc:creator><description>&lt;p&gt;(This algorithm erroneously reports that no files require one disk. Fixing that is left as an exercise.)&lt;/p&gt;
&lt;p&gt;change&lt;/p&gt;
&lt;p&gt;set DISKSIZE=1474560&lt;/p&gt;
&lt;p&gt;set CLUSTER=512&lt;/p&gt;
&lt;p&gt;set DISKS=1&lt;/p&gt;
&lt;p&gt;set TOTAL=0&lt;/p&gt;
&lt;p&gt;to&lt;/p&gt;
&lt;p&gt;set DISKSIZE=1474560&lt;/p&gt;
&lt;p&gt;set CLUSTER=512&lt;/p&gt;
&lt;p&gt;set DISKS=0&lt;/p&gt;
&lt;p&gt;set TOTAL=%DISKSIZE%&lt;/p&gt;
&lt;p&gt;so set the imaginary 0 disk to be full such that teh firt fil will alwasy &amp;#39;not fit&amp;#39; and overflow to first disk.&lt;/p&gt;
&lt;p&gt;:D&lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=10336396" width="1" height="1"&gt;</description></item><item><title>re: Exiting a batch file without exiting the command shell -and- batch file subroutines</title><link>http://blogs.msdn.com/b/oldnewthing/archive/2012/08/02/10334559.aspx#10336342</link><pubDate>Thu, 02 Aug 2012 21:21:43 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:10336342</guid><dc:creator>Joshua</dc:creator><description>&lt;p&gt;@Skyborne: I can reduce away this Knapsack problem easily if it were worth my while.&lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=10336342" width="1" height="1"&gt;</description></item><item><title>re: Exiting a batch file without exiting the command shell -and- batch file subroutines</title><link>http://blogs.msdn.com/b/oldnewthing/archive/2012/08/02/10334559.aspx#10336335</link><pubDate>Thu, 02 Aug 2012 20:55:20 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:10336335</guid><dc:creator>voo</dc:creator><description>&lt;p&gt;@Skyborne Think so too. So clearly the next step is dynamic programming with batch files! &lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=10336335" width="1" height="1"&gt;</description></item><item><title>re: Exiting a batch file without exiting the command shell -and- batch file subroutines</title><link>http://blogs.msdn.com/b/oldnewthing/archive/2012/08/02/10334559.aspx#10336308</link><pubDate>Thu, 02 Aug 2012 19:42:38 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:10336308</guid><dc:creator>Skyborne</dc:creator><description>&lt;p&gt;@pinwing, I think we&amp;#39;re looking at an instance of the Knapsack Problem, in which case finding the optimal packing knowing everything beforehand is NP-complete... and the batch file only looks at items streaming past one loop at a time.&lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=10336308" width="1" height="1"&gt;</description></item><item><title>re: Exiting a batch file without exiting the command shell -and- batch file subroutines</title><link>http://blogs.msdn.com/b/oldnewthing/archive/2012/08/02/10334559.aspx#10336249</link><pubDate>Thu, 02 Aug 2012 17:11:14 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:10336249</guid><dc:creator>pinwing</dc:creator><description>&lt;p&gt;Hmm, the number of floppies seem to be at least 2.&lt;/p&gt;
&lt;p&gt;I guess DISK should be initialized to 1?&lt;/p&gt;
&lt;p&gt;Also, if I have files a.txt (1 byte), b.txt (1474560-512 bytes) and c.txt (1 byte), then it may come to the conclusion that all files need on their own floppy (instead of putting a.txt and c.txt together).&lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=10336249" width="1" height="1"&gt;</description></item><item><title>re: Exiting a batch file without exiting the command shell -and- batch file subroutines</title><link>http://blogs.msdn.com/b/oldnewthing/archive/2012/08/02/10334559.aspx#10336239</link><pubDate>Thu, 02 Aug 2012 17:04:51 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:10336239</guid><dc:creator>skSdnW</dc:creator><description>&lt;p&gt;A big problem with exiting batch files is that they don&amp;#39;t set the exit/error code used by cmd.exe for &amp;amp;&amp;amp; and || handling:&lt;/p&gt;
&lt;p&gt;test.cmd:&lt;/p&gt;
&lt;p&gt;@echo off&lt;/p&gt;
&lt;p&gt;call Fail\This\Line.exe&lt;/p&gt;
&lt;p&gt;if errorlevel 1 (echo.ErrorMessageHere&amp;amp;exit /b 1)&lt;/p&gt;
&lt;p&gt;echo Should not get here&lt;/p&gt;
&lt;p&gt;Then in cmd.exe run: test&amp;amp;&amp;amp;echo.OK||echo.Failed&lt;/p&gt;
&lt;p&gt;To fix it you can use ugly workarounds:&lt;/p&gt;
&lt;p&gt;@echo off&lt;/p&gt;
&lt;p&gt;call Fail\This\Line.exe&lt;/p&gt;
&lt;p&gt;if errorlevel 1 (echo.ErrorMessageHere&amp;amp;goto dieerrlvl)&lt;/p&gt;
&lt;p&gt;echo Should not get here&lt;/p&gt;
&lt;p&gt;:dieerrlvl&lt;/p&gt;
&lt;p&gt;%comspec% /c exit %errorlevel%&lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=10336239" width="1" height="1"&gt;</description></item><item><title>re: Exiting a batch file without exiting the command shell -and- batch file subroutines</title><link>http://blogs.msdn.com/b/oldnewthing/archive/2012/08/02/10334559.aspx#10336231</link><pubDate>Thu, 02 Aug 2012 16:52:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:10336231</guid><dc:creator>Stefan Kanthak</dc:creator><description>&lt;p&gt;EXIT and EXIT /B have another advantage over GOTO :EOF&lt;/p&gt;
&lt;p&gt;they can return an errorlevel.&lt;/p&gt;
&lt;p&gt;BUT: dont try to use IF ERRORLEVEL with negative values&lt;/p&gt;
&lt;p&gt;--- ERRORLEVEL.CMD ---&lt;/p&gt;
&lt;p&gt;%SystemRoot%\System32\Cmd.Exe /C Exit -1&lt;/p&gt;
&lt;p&gt;Echo ERRORLEVEL: %ERRORLEVEL%&lt;/p&gt;
&lt;p&gt;If ERRORLEVEL 0 (Echo OK [0 ^&amp;gt; -1]) Else (Echo ERROR [0 ^&amp;lt;= -1])&lt;/p&gt;
&lt;p&gt;--- eof ---&lt;/p&gt;
&lt;p&gt;@Raymond: it doesnt hurt to write always GOTO :label&lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=10336231" width="1" height="1"&gt;</description></item><item><title>re: Exiting a batch file without exiting the command shell -and- batch file subroutines</title><link>http://blogs.msdn.com/b/oldnewthing/archive/2012/08/02/10334559.aspx#10336218</link><pubDate>Thu, 02 Aug 2012 16:20:18 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:10336218</guid><dc:creator>Programmerman</dc:creator><description>&lt;p&gt;It does feel like the single greatest skill a batch file writer can have is complete knowledge of the FOR command and all the ways it can be abused.&lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=10336218" width="1" height="1"&gt;</description></item></channel></rss>