{"id":34263,"date":"2005-09-09T10:00:24","date_gmt":"2005-09-09T10:00:24","guid":{"rendered":"https:\/\/blogs.msdn.microsoft.com\/oldnewthing\/2005\/09\/09\/reading-the-output-of-a-command-from-batch\/"},"modified":"2005-09-09T10:00:24","modified_gmt":"2005-09-09T10:00:24","slug":"reading-the-output-of-a-command-from-batch","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/oldnewthing\/20050909-24\/?p=34263","title":{"rendered":"Reading the output of a command from batch"},"content":{"rendered":"<p><P>\nThe <CODE>FOR<\/CODE> command has become the batch language&#8217;s\nlooping construct.\nIf you ask for help via <CODE>FOR \/?<\/CODE> you can see all the\nways it has become overloaded.\nFor example,\nyou can read the output of a command\nby using the <CODE>for<\/CODE> command.\n<\/P>\n<PRE>\nFOR \/F &#8220;tokens=*&#8221; %i IN (&#8216;ver&#8217;) DO echo %i\n<\/PRE>\n<P>\nThe <\/CODE>\/F<\/CODE> switch in conjunction with the single quotation\nmarks indicates that the quoted string is a command to run, whose output\nis then to be parsed and returned in the specified variable (or variables).\nThe option <CODE>&#8220;tokens=*&#8221;<\/CODE> says that the entire line should be\ncollected.  There are several other options that control the parsing,\nwhich I leave you to read on your own.\n<\/P>\n<P>\nThe kludgy batch language gets even kludgier.\nWhy is the batch language such a grammatical mess?\nBackwards compatibility.\n<\/P>\n<P>\nAny change to the batch language cannot break compatibility with the\nmillions of batch programs already in existence.\nSuch batch files are burned onto millions of CDs\n(you&#8217;d be surprised how many commercial programs use batch files,\nparticularly as part of their installation process).\nThey&#8217;re also run by corporations around the world\nto get their day-to-day work done.\nPlus of course the batch files written by people like you and me\nto do a wide variety of things.\nAny change to the batch language must keep these batch files running.\n<\/P>\n<P>\nOf course, one could invent a brand new batch language,\nlet&#8217;s call it Batch&#xB2; for the sake of discussion, and thereby\nbe rid of the backwards compatibility constraints.\nBut with that decision come different obstacles.\n<\/P>\n<P>\nSuppose you have a 500-line batch file and you want to add one little\nfeature to it, but that new feature is available only in Batch&#xB2;.\nDoes this mean that you have to do a complete rewrite of your batch\nprogram into Batch&#xB2;?\nYour company spent years tweaking this batch file over the years.\n(And by &#8220;tweaking&#8221; I might mean &#8220;turning into a plate of spaghetti&#8221;.)\nDo you want to take the risk of introducing who-knows-how-many bugs\nand breaking various obscure features as part of the rewrite into\nBatch&#xB2;?\n<\/P>\n<P>\nSuppose you decide to bite the bullet and rewrite.\nOh, but Batch&#xB2; is available only in more recent\nversions of Windows.\nDo you tell your customers, &#8220;We don&#8217;t support the older versions of\nWindows any more&#8221;?\nOr do you bite another bullet and say, &#8220;We support only versions of\nWindows that have Batch&#xB2;&#8221;?\n<\/P>\n<P>\nI&#8217;m not saying that it won&#8217;t happen.\n(In fact, I&#8217;m under the impression that there are already efforts to\ndesign\n<A HREF=\"https:\/\/channel9.msdn.com\/wiki\/default.aspx\/Channel9.MSHWiki\">\na new command console language with an entirely new grammar<\/A>.\nSaid effort\n<A HREF=\"http:\/\/commnet.microsoftpdc.com\/content\/sessionview.aspx?TopicID=1bbfa7e1-d4a7-4556-9e52-680bb143f8d6\">\nmight even be presenting at the PDC in a few days<\/A>.)\nI&#8217;m just explaining why the classic batch language is such a mess.\nWelcome to evolution.\n<\/P>\n<P>\n[Raymond is currently away; this message was pre-recorded.]\n<\/P><\/p>\n","protected":false},"excerpt":{"rendered":"<p>The FOR command has become the batch language&#8217;s looping construct. If you ask for help via FOR \/? you can see all the ways it has become overloaded. For example, you can read the output of a command by using the for command. FOR \/F &#8220;tokens=*&#8221; %i IN (&#8216;ver&#8217;) DO echo %i The \/F switch [&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-34263","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-oldnewthing","tag-code"],"acf":[],"blog_post_summary":"<p>The FOR command has become the batch language&#8217;s looping construct. If you ask for help via FOR \/? you can see all the ways it has become overloaded. For example, you can read the output of a command by using the for command. FOR \/F &#8220;tokens=*&#8221; %i IN (&#8216;ver&#8217;) DO echo %i The \/F switch [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/34263","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=34263"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/34263\/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=34263"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/categories?post=34263"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/tags?post=34263"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}