{"id":4258,"date":"2013-01-25T12:18:21","date_gmt":"2013-01-25T17:18:21","guid":{"rendered":"https:\/\/blogs.mathworks.com\/pick\/?p=4258"},"modified":"2013-01-25T12:18:21","modified_gmt":"2013-01-25T17:18:21","slug":"recursive-directory-searching-for-multiple-file-specs","status":"publish","type":"post","link":"https:\/\/blogs.mathworks.com\/pick\/2013\/01\/25\/recursive-directory-searching-for-multiple-file-specs\/","title":{"rendered":"Recursive directory searching for multiple file specs?"},"content":{"rendered":"<div xmlns:mwsh=\"https:\/\/www.mathworks.com\/namespace\/mcode\/v1\/syntaxhighlight.dtd\" class=\"content\">\r\n   <introduction><\/introduction>\r\n   <h3>Contents<\/h3>\r\n   <div>\r\n      <ul>\r\n         <li><a href=\"#2\">A Recursive DIR Command that Accommodates a Complex Filterspec<\/a><\/li>\r\n         <li><a href=\"#6\">Did I Miss Something?<\/a><\/li>\r\n         <li><a href=\"#7\">So Here's the Challenge<\/a><\/li>\r\n      <\/ul>\r\n   <\/div>\r\n   <p><a href=\"https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/authors\/911\">Brett<\/a>'s Pick this week is ... nonexistent. (Instead, here's another challenge.)\r\n   <\/p>\r\n   <h3>A Recursive DIR Command that Accommodates a Complex Filterspec<a name=\"2\"><\/a><\/h3>\r\n   <p>For a project that I'm currently working on, I want to return a directory listing of all files matching one of several specified\r\n      formats. I wanted to include in my directory search, for example, all files of type 'doc', 'xml', or 'html'. I can easily\r\n      <i>select<\/i> such files using MATLAB's <a href=\"https:\/\/www.mathworks.com\/help\/releases\/R2012b\/matlab\/ref\/uigetfile.html\"><tt>uigetfile<\/tt><\/a> command:\r\n   <\/p><pre>[filename, pathname] = uigetfile({'*.doc;*.xml;*.html','Brett''s File Formats';...\r\n        '*.*','All Files' },'Brett''s Search',...\r\n        'C:\\Brett\\Miscellaneous Documents\\myFile.xml',...\r\n        'multiselect','on')<\/pre><p>But listing those files in a <a href=\"https:\/\/www.mathworks.com\/help\/releases\/R2012b\/matlab\/ref\/dir.html\"><tt>dir<\/tt><\/a>-like format is another (more challenging) matter. Moreover, just to complicate the issue, I want to be able to specify recursive\r\n      or non-recursive searching--that is, to tell MATLAB to include or exclude, respectively, subdirectories below my initial search\r\n      path.\r\n   <\/p>\r\n   <p>The <tt>dir<\/tt> command doesn't support that capability.\r\n   <\/p>\r\n   <p>Rather than write my own custom directory-listing code, my first thought was to search the File Exchange to see if someone\r\n      had already done so. I was not (initially) disappointed; there are several submissions that <i>appear<\/i> to do what I need the function to do. (What an amazing resource the File Exchange is!) In fact, there were so many promising\r\n      files on the Exchange that I decided to get even more restrictive in my criteria. Specifically, I wanted to find a directory-searching\r\n      function that:\r\n   <\/p>\r\n   <div>\r\n      <ol>\r\n         <li>can search for multiple file formats;<\/li>\r\n         <li>can search recursively or non-recursively;<\/li>\r\n         <li>provides a command-line (non-GUI) interface;<\/li>\r\n         <li>works \"out-of-the-box\" in R2012b--without requiring a lot of effort on my part;<\/li>\r\n         <li>returns results in the same format that the <tt>dir<\/tt> command returns; and\r\n         <\/li>\r\n         <li>is shared under a BSD license.<\/li>\r\n      <\/ol>\r\n   <\/div>\r\n   <p>Would I have any luck with these more restrictive requirements?<\/p>\r\n   <p>As it turns out, not so much. I found many files that satisfy some of my criteria, but none that satisfies them all. Armed\r\n      with that knowledge, and with some insights I had gained by looking at some other contributors' code, I figured that I could\r\n      either relax my criteria (some of them are \"must-haves,\" others are just niceties), or I could write my own function. But\r\n      then I had another idea: this is great fodder for a blog post!  Why not challenge readers to create a function to meet those\r\n      specs? (How far are you willing to go for MATLAB swag, and for recognition in the Pick-of-the-Week blog?) :)\r\n   <\/p>\r\n   <h3>Did I Miss Something?<a name=\"6\"><\/a><\/h3>\r\n   <p>First, if I overlooked (or misjudged) a particular submission, please <a href=\"https:\/\/blogs.mathworks.com\/pick\/?p=4258#respond\">let me know<\/a>. If I agree that a file currently on the exchange meets all of the criteria listed above, I'll recognize your file publicly,\r\n      apologize humbly, and send you some swag! It's quite possible that I missed something.\r\n   <\/p>\r\n   <h3>So Here's the Challenge<a name=\"7\"><\/a><\/h3>\r\n   <p>I will send some swag to the first person who shares on the File Exchange some code that meets the six criteria specified\r\n      above. You can start from scratch, or you can modify an existing file--yours, or someone else's. And I will feature your submission\r\n      as a Pick-of-the-Week in a future blog.\r\n   <\/p>\r\n   <p>As always, I welcome your <a href=\"https:\/\/blogs.mathworks.com\/pick\/?p=4258#respond\">thoughts and comments<\/a>.\r\n   <\/p><script language=\"JavaScript\">\r\n<!--\r\n\r\n    function grabCode_b7a95349c91b4b9ab93035c0f5513457() {\r\n        \/\/ Remember the title so we can use it in the new page\r\n        title = document.title;\r\n\r\n        \/\/ Break up these strings so that their presence\r\n        \/\/ in the Javascript doesn't mess up the search for\r\n        \/\/ the MATLAB code.\r\n        t1='b7a95349c91b4b9ab93035c0f5513457 ' + '##### ' + 'SOURCE BEGIN' + ' #####';\r\n        t2='##### ' + 'SOURCE END' + ' #####' + ' b7a95349c91b4b9ab93035c0f5513457';\r\n    \r\n        b=document.getElementsByTagName('body')[0];\r\n        i1=b.innerHTML.indexOf(t1)+t1.length;\r\n        i2=b.innerHTML.indexOf(t2);\r\n \r\n        code_string = b.innerHTML.substring(i1, i2);\r\n        code_string = code_string.replace(\/REPLACE_WITH_DASH_DASH\/g,'--');\r\n\r\n        \/\/ Use \/x3C\/g instead of the less-than character to avoid errors \r\n        \/\/ in the XML parser.\r\n        \/\/ Use '\\x26#60;' instead of '<' so that the XML parser\r\n        \/\/ doesn't go ahead and substitute the less-than character. \r\n        code_string = code_string.replace(\/\\x3C\/g, '\\x26#60;');\r\n\r\n        author = 'Brett Shoelson';\r\n        copyright = 'Copyright 2013 The MathWorks, Inc.';\r\n\r\n        w = window.open();\r\n        d = w.document;\r\n        d.write('<pre>\\n');\r\n        d.write(code_string);\r\n\r\n        \/\/ Add author and copyright lines at the bottom if specified.\r\n        if ((author.length > 0) || (copyright.length > 0)) {\r\n            d.writeln('');\r\n            d.writeln('%%');\r\n            if (author.length > 0) {\r\n                d.writeln('% _' + author + '_');\r\n            }\r\n            if (copyright.length > 0) {\r\n                d.writeln('% _' + copyright + '_');\r\n            }\r\n        }\r\n\r\n        d.write('<\/pre>\\n');\r\n      \r\n      d.title = title + ' (MATLAB code)';\r\n      d.close();\r\n      }   \r\n      \r\n-->\r\n<\/script><p style=\"text-align: right; font-size: xx-small; font-weight:lighter;   font-style: italic; color: gray\"><br><a href=\"javascript:grabCode_b7a95349c91b4b9ab93035c0f5513457()\"><span style=\"font-size: x-small;        font-style: italic;\">Get \r\n            the MATLAB code \r\n            <noscript>(requires JavaScript)<\/noscript><\/span><\/a><br><br>\r\n      Published with MATLAB&reg; R2013a<br><\/p>\r\n<\/div>\r\n<!--\r\nb7a95349c91b4b9ab93035c0f5513457 ##### SOURCE BEGIN #####\r\n%% Recursive directory searching for multiple file specs?\r\n\r\n%% \r\n% <https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/authors\/911 Brett>'s Pick this week is\r\n% ... nonexistent. (Instead, here's another challenge.)\r\n\r\n%% A Recursive |DIR| Command that Accommodates a Complex Filterspec\r\n% For a project that I'm currently working on, I want to return a directory listing\r\n% of all files matching one of several specified formats. I\r\n% wanted to include in my directory search, for example, all files of type 'doc',\r\n% 'xml', or 'html'. I can easily _select_ such files using\r\n% MATLAB's <https:\/\/www.mathworks.com\/help\/releases\/R2012b\/matlab\/ref\/uigetfile.html |uigetfile|>\r\n% command:\r\n%\r\n%  [filename, pathname] = uigetfile({'*.doc;*.xml;*.html','Brett''s File Formats';...\r\n%          '*.*','All Files' },'Brett''s Search',...\r\n%          'C:\\Brett\\Miscellaneous Documents\\myFile.xml',...\r\n%          'multiselect','on')\r\n%\r\n% But listing those files in a \r\n% <https:\/\/www.mathworks.com\/help\/releases\/R2012b\/matlab\/ref\/dir.html |dir|>-like format\r\n% is another (more challenging) matter. Moreover, just to complicate the issue,\r\n% I want to be able to specify recursive or non-recursive searchingREPLACE_WITH_DASH_DASHthat is, to \r\n% tell MATLAB to include or exclude, respectively, subdirectories\r\n% below my initial search path.\r\n\r\n%%\r\n% The |dir| command doesn't support that capability.\r\n\r\n%% \r\n% Rather than write my own custom directory-listing code, my first thought was to search the File\r\n% Exchange to see if someone had already done so. I was not (initially) disappointed; there are several submissions that\r\n% _appear_ to do what I need the function to do. (What an amazing resource the File Exchange is!)\r\n% In fact, there were so many promising files on the Exchange that I decided to get even more\r\n% restrictive in my criteria. Specifically, I wanted to find a directory-searching function\r\n% that:\r\n% \r\n% # can search for multiple file formats;\r\n% # can search recursively or non-recursively; \r\n% # provides a command-line (non-GUI) interface;\r\n% # works \"out-of-the-box\" in R2012bREPLACE_WITH_DASH_DASHwithout requiring a lot of effort on my part;\r\n% # returns results in the same format that the |dir| command returns; and\r\n% # is shared under a BSD license.\r\n%\r\n% Would I have any luck with these more restrictive requirements?\r\n\r\n%%\r\n% As it turns out, not so much. I found many files that satisfy some of my\r\n% criteria, but none that satisfies them all. Armed with that knowledge, and with some insights I had \r\n% gained by looking at some other contributors' code, I figured that I could either relax my\r\n% criteria (some of them are \"must-haves,\" others are just niceties), or I could write my own function.\r\n% But then I had another idea: this is great fodder for a blog post!  Why not\r\n% challenge readers to create a function to meet those specs? (How far are you willing to go for \r\n% MATLAB swag, and for recognition in the Pick-of-the-Week blog?) :)\r\n\r\n%% Did I Miss Something?\r\n% First, if I overlooked (or misjudged) a particular submission, please\r\n% <https:\/\/blogs.mathworks.com\/pick\/?p=4258#respond let me know>. If I agree that \r\n% a file currently on the exchange meets all of the criteria listed above, I'll \r\n% recognize your file publicly, apologize humbly, and send you some swag!\r\n% It's quite possible that I missed something.\r\n\r\n%% So Here's the Challenge\r\n% I will send some swag to the first person who shares on the File Exchange\r\n% some code that meets the six criteria specified above. You can start from\r\n% scratch, or you can modify an existing fileREPLACE_WITH_DASH_DASHyours, or someone else's.\r\n% And I will feature your submission as a Pick-of-the-Week in a future\r\n% blog. \r\n\r\n%% \r\n% As always, I welcome your <https:\/\/blogs.mathworks.com\/pick\/?p=4258#respond thoughts and comments>.\r\n##### SOURCE END ##### b7a95349c91b4b9ab93035c0f5513457\r\n-->","protected":false},"excerpt":{"rendered":"<p>\r\n   \r\n   Contents\r\n   \r\n      \r\n         A Recursive DIR Command that Accommodates a Complex Filterspec\r\n         Did I Miss Something?\r\n    ... <a class=\"read-more\" href=\"https:\/\/blogs.mathworks.com\/pick\/2013\/01\/25\/recursive-directory-searching-for-multiple-file-specs\/\">read more >><\/a><\/p>","protected":false},"author":34,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[16],"tags":[],"_links":{"self":[{"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/posts\/4258"}],"collection":[{"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/users\/34"}],"replies":[{"embeddable":true,"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/comments?post=4258"}],"version-history":[{"count":5,"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/posts\/4258\/revisions"}],"predecessor-version":[{"id":4266,"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/posts\/4258\/revisions\/4266"}],"wp:attachment":[{"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/media?parent=4258"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/categories?post=4258"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/tags?post=4258"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}