{"id":148,"date":"2008-07-15T13:47:23","date_gmt":"2008-07-15T21:47:23","guid":{"rendered":"http:\/\/www.curlybrace.com\/words\/?p=148"},"modified":"2010-12-15T14:31:23","modified_gmt":"2010-12-15T22:31:23","slug":"windows-batch-files-if-conditional-with-echoed-parentheses","status":"publish","type":"post","link":"https:\/\/www.curlybrace.com\/words\/2008\/07\/windows-batch-files-if-conditional-with-echoed-parentheses\/","title":{"rendered":"Weirdness With Echo and Parentheses Within Conditional Statements"},"content":{"rendered":"<p>The <tt>if<\/tt> command in Windows batch files has some strange implications.  One problem is that parentheses take on special meaning.<\/p>\n<p>I first ran into this problem because my batch file would terminate shortly after showing, &#8220;<tt>folder unexpected at this time<\/tt>&#8220;.<\/p>\n<p>A very basic <tt>if<\/tt> command might look like this:<\/p>\n<blockquote>\n<pre class=\"DOS\">C:\\>if exist C:\\ ( echo The C drive exists )\r\nThe C drive exists<\/pre>\n<\/blockquote>\n<p>However, placing parentheses inside the echo statement will entirely confuse CMD:<\/p>\n<blockquote>\n<pre class=\"DOS\">C:\\>if exist C:\\ ( echo The C drive exists (no, really) )\r\n) was unexpected at this time.<\/pre>\n<\/blockquote>\n<p>Well, that sort of makes sense.  It becomes confusing when the last word in your echo command is something more general, like the word &#8216;folder&#8217;:<\/p>\n<blockquote>\n<pre class=\"DOS\">C:\\>if exist C:\\Windows ( echo Found the Windows (on C:) folder )\r\nfolder was unexpected at this time.<\/pre>\n<\/blockquote>\n<p>This is actually the classic <a href=\"http:\/\/www.google.com\/search?source=ig&#038;hl=en&#038;rlz=1G1GGLQ_ENUS258&#038;=&#038;q=%22was+unexpected+at+this+time%22&#038;btnG=Google+Search\"><tt>%%i was unexpected at this time<\/tt> error<\/a>.<\/p>\n<p>One solution is to put double quotes around the message to be echoed:<\/p>\n<blockquote>\n<pre class=\"DOS\">C:\\>if exist C:\\Windows ( echo \"Found the Windows (on C:) folder\" )\r\n\"Found the Windows (on C:) folder\"<\/pre>\n<\/blockquote>\n<p>The drawback here is that the double quotes appear in the output.  A better option is to use the caret (^) escape character:<\/p>\n<blockquote>\n<pre class=\"DOS\">C:\\>if exist C:\\Windows ( echo Found the Windows ^(on C:^) folder )\r\nFound the Windows (on C:) folder<\/pre>\n<\/blockquote>\n","protected":false},"excerpt":{"rendered":"<p>The if command in Windows batch files has some strange implications. One problem is that parentheses take on special meaning. I first ran into this problem because my batch file would terminate shortly after showing, &#8220;folder unexpected at this time&#8220;. &hellip; <a href=\"https:\/\/www.curlybrace.com\/words\/2008\/07\/windows-batch-files-if-conditional-with-echoed-parentheses\/\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":3,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[15],"tags":[],"class_list":["post-148","post","type-post","status-publish","format-standard","hentry","category-technology"],"_links":{"self":[{"href":"https:\/\/www.curlybrace.com\/words\/wp-json\/wp\/v2\/posts\/148","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.curlybrace.com\/words\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.curlybrace.com\/words\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.curlybrace.com\/words\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/www.curlybrace.com\/words\/wp-json\/wp\/v2\/comments?post=148"}],"version-history":[{"count":2,"href":"https:\/\/www.curlybrace.com\/words\/wp-json\/wp\/v2\/posts\/148\/revisions"}],"predecessor-version":[{"id":1407,"href":"https:\/\/www.curlybrace.com\/words\/wp-json\/wp\/v2\/posts\/148\/revisions\/1407"}],"wp:attachment":[{"href":"https:\/\/www.curlybrace.com\/words\/wp-json\/wp\/v2\/media?parent=148"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.curlybrace.com\/words\/wp-json\/wp\/v2\/categories?post=148"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.curlybrace.com\/words\/wp-json\/wp\/v2\/tags?post=148"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}