{"id":20743,"date":"2008-09-26T10:00:00","date_gmt":"2008-09-26T10:00:00","guid":{"rendered":"https:\/\/blogs.msdn.microsoft.com\/oldnewthing\/2008\/09\/26\/errorlevel-is-not-errorlevel\/"},"modified":"2008-09-26T10:00:00","modified_gmt":"2008-09-26T10:00:00","slug":"errorlevel-is-not-errorlevel","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/oldnewthing\/20080926-00\/?p=20743","title":{"rendered":"ERRORLEVEL is not %ERRORLEVEL%"},"content":{"rendered":"<p><P>\nThe command interpreter <CODE>cmd.exe<\/CODE> has a concept\nknown as the <I>error level<\/I>,\nwhich is the exit code of the program most recently run.\nYou can test the error level with the\n<CODE>IF ERRORLEVEL<\/CODE> command:\n<\/P>\n<PRE>\nIF ERRORLEVEL 1 ECHO error level is 1 or more\n<\/PRE>\n<P>\n&lt;sidebar&gt;<BR>\nThe <CODE>IF ERRORLEVEL n<\/CODE> test succeeds\nif the error level is n <I>or more<\/I>.\nThis was presumably because there were programs that\nexpressed different degrees of failure with higher and\nhigher exit codes.\nFor example, the <CODE>diff<\/CODE> program has three exit codes:\n0 means the files are the same;\n1 means the files are different;\n2 means that something terrible happened.\nThere are also programs that use an exit code of zero\nto mean success and anything else to mean failure.<BR>\n&lt;\/sidebar&gt;\n<\/P>\n<P>\nIn addition to this internal state,\nyou can, if you wish, create an environment variable\nwith the name <CODE>ERRORLEVEL<\/CODE>,\nin the same way that you can create an environment variable\ncalled <CODE>FRED<\/CODE>.\nBut, as with <CODE>FRED<\/CODE>,\nthat variable won&#8217;t have any effect on the error level.\n<\/P>\n<PRE>\nrem this next command sets the error level to zero\nCMD \/C EXIT 0\nset ERRORLEVEL=1\nif ERRORLEVEL 1 echo Does this print?\n<\/PRE>\n<P>\nThe message is not printed because the <CODE>ERRORLEVEL<\/CODE>\nenvironment variable has no effect on the error level.\nIt&#8217;s just a variable whose name happens to coincide with a command\nprocessor concept.\n<\/P>\n<PRE>\nset BANKBALANCE=$1,000,000.00\n<\/PRE>\n<P>\n&#8220;Hey, when I tried to withdraw the money, I got an insufficient funds error.\nWhat am I doing wrong?&#8221;\n<\/P>\n<P>\nNow, it does happen to be the case that if command extensions are\nenabled and you say\n<CODE>%ERRORLEVEL%<\/CODE>, then the command processor first looks\nfor an environment variable called <CODE>ERRORLEVEL<\/CODE>,\nand <I>if it can&#8217;t find one<\/I>, then it replaces <CODE>%ERRORLEVEL%<\/CODE>\nwith the current value of the internal error level value.\nIt&#8217;s a fallback step,\nin the same way that your neighbor is a fallback delivery location if\nyou aren&#8217;t home.\nIf you file a change-of-address form for yourself, that doesn&#8217;t affect\npackages sent to your neighbor.\n<\/P>\n<P>\nThe same behavior can be seen with <CODE>%CD%<\/CODE>:\nIf you did not explicitly set an environment variable called <CODE>CD<\/CODE>,\nthen <CODE>%CD%<\/CODE> expands to the command processor&#8217;s current\ndirectory.\nBut you can&#8217;t change directories by saying <CODE>set CD=C:\\Windows<\/CODE>.\n<\/P>\n<P>\nI can think of a few reasons why this feature may have been added.\n<\/P>\n<UL>\n<LI>So you can include the error level in a log file:<BR>\n    <CODE>ECHO error level is %ERRORLEVEL%&gt;logfile<\/CODE>\n<LI>So you can perform other types of tests against the error level,\n    for example, to perform an equality test:<BR>\n    <CODE>IF %ERRORLEVEL% EQU 1 echo Different!<\/CODE>\n<\/UL>\n<P>\nBut I&#8217;m digressing.\nMy point for today is that the error level is not the same\nas the <CODE>ERRORLEVEL<\/CODE> environment variable.\n<\/P><\/p>\n","protected":false},"excerpt":{"rendered":"<p>The command interpreter cmd.exe has a concept known as the error level, which is the exit code of the program most recently run. You can test the error level with the IF ERRORLEVEL command: IF ERRORLEVEL 1 ECHO error level is 1 or more &lt;sidebar&gt; The IF ERRORLEVEL n test succeeds if the error level [&hellip;]<\/p>\n","protected":false},"author":1069,"featured_media":111744,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1],"tags":[25],"class_list":["post-20743","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-oldnewthing","tag-code"],"acf":[],"blog_post_summary":"<p>The command interpreter cmd.exe has a concept known as the error level, which is the exit code of the program most recently run. You can test the error level with the IF ERRORLEVEL command: IF ERRORLEVEL 1 ECHO error level is 1 or more &lt;sidebar&gt; The IF ERRORLEVEL n test succeeds if the error level [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/20743","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/users\/1069"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/comments?post=20743"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/20743\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/media\/111744"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/media?parent=20743"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/categories?post=20743"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/tags?post=20743"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}