{"id":26893,"date":"2007-05-11T10:00:00","date_gmt":"2007-05-11T10:00:00","guid":{"rendered":"https:\/\/blogs.msdn.microsoft.com\/oldnewthing\/2007\/05\/11\/performing-an-operation-in-each-subdirectory-of-a-directory-tree-from-batch\/"},"modified":"2007-05-11T10:00:00","modified_gmt":"2007-05-11T10:00:00","slug":"performing-an-operation-in-each-subdirectory-of-a-directory-tree-from-batch","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/oldnewthing\/20070511-00\/?p=26893","title":{"rendered":"Performing an operation in each subdirectory of a directory tree from batch"},"content":{"rendered":"<p>\nTo execute a command in each subdirectory of a directory tree\nfrom a batch file, you can adapt the following:\n<\/p>\n<pre>\nfor \/f \"delims=\" %%i in ('dir \/ad\/s\/b') do echo %%i\n<\/pre>\n<p>\n(If you want to play with this command from the command prompt,\nthen undouble the percent signs.)\n<\/p>\n<p>\nThe <code>\/F<\/code> option enables various special behaviors\nof the <code>FOR<\/code> command.\nThe most important change is that a string in single-quotation marks\ncauses the contents to be interpreted as a command whose output is\nto be parsed.\n(This behavior changes if you use the <code>usebackq<\/code> option,\nbut I&#8217;m not using that here.)\nTherefore, the <code>FOR<\/code> command will run the\n<code>dir \/ad\/s\/b<\/code> command and parse the output.\nThe <code>dir \/ad\/s\/b<\/code> command performs a recursive listing\nof only directories, printing just the names of the directories found.\n<\/p>\n<p>\nThe option we provide, <code>delims=<\/code> changes the default\ndelimiter from a space to nothing.\nThis means that the entire line is to be read into the <code>%i<\/code> variable.\n(Normally, only the first word is assigned to <code>%i<\/cODE>.)\nTherefore, the <code>FOR<\/code> loop executes once for each subdirectory,\nwith the <code>%i<\/code> variable set to the subdirectory name.\n<\/p>\n<p>\nThe command request to be performed for each line is simply echoing the\ndirectory name.\nIn real life, you would probably put something more interesting here.\nFor example, to dump the security descriptor of each\ndirectory (which was the original problem that inspired this entry),\nyou can type this on the command line:\n<\/p>\n<pre>\nfor \/f \"delims=\" %i in ('dir \/ad\/s\/b') do cacls \"%i\" &gt;&gt;\"%TEMP%\\cacls.log\"\n<\/pre>\n<p>\n<b>Nitpicker's corner<\/b>\n<\/p>\n<p>\nI doubt anybody actually <i>enjoys<\/i> working with batch files,\nbut that doesn't mean tips on using it more effectively aren't valid.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>To execute a command in each subdirectory of a directory tree from a batch file, you can adapt the following: for \/f &#8220;delims=&#8221; %%i in (&#8216;dir \/ad\/s\/b&#8217;) do echo %%i (If you want to play with this command from the command prompt, then undouble the percent signs.) The \/F option enables various special behaviors of [&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":[104],"class_list":["post-26893","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-oldnewthing","tag-tipssupport"],"acf":[],"blog_post_summary":"<p>To execute a command in each subdirectory of a directory tree from a batch file, you can adapt the following: for \/f &#8220;delims=&#8221; %%i in (&#8216;dir \/ad\/s\/b&#8217;) do echo %%i (If you want to play with this command from the command prompt, then undouble the percent signs.) The \/F option enables various special behaviors of [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/26893","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=26893"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/26893\/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=26893"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/categories?post=26893"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/tags?post=26893"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}