{"id":11275,"date":"2020-01-10T11:18:07","date_gmt":"2020-01-10T16:18:07","guid":{"rendered":"https:\/\/blogs.mathworks.com\/pick\/?p=11275"},"modified":"2020-01-10T11:19:18","modified_gmt":"2020-01-10T16:19:18","slug":"determining-next-available-file-name","status":"publish","type":"post","link":"https:\/\/blogs.mathworks.com\/pick\/2020\/01\/10\/determining-next-available-file-name\/","title":{"rendered":"Determining next available file name"},"content":{"rendered":"\r\n<div class=\"content\"><p><a href=\"http:\/\/www.mathworks.com\/matlabcentral\/profile\/authors\/869871\">Jiro<\/a>&#8216;s Pick this week is <a href=\"https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/64108\"><tt>nextname<\/tt><\/a> by <a href=\"https:\/\/www.mathworks.com\/matlabcentral\/profile\/authors\/3102170\">Stephen Cobeldick<\/a>.<\/p><p>Stephen is no stranger in the MATLAB user community arena. As of the writing of this post, Stephen ranks 44 out of over 15,000 File Exchange users. He also ranks 5 out of over 190,000 MATLAB Answers users. One of his files have been Picked not <a href=\"https:\/\/blogs.mathworks.com\/pick\/2015\/01\/09\/spelling-out-numbers\/\">once<\/a>, not <a href=\"https:\/\/blogs.mathworks.com\/pick\/2019\/04\/26\/convert-between-rgb-and-color-names\/\">twice<\/a>, but <a href=\"https:\/\/blogs.mathworks.com\/pick\/2014\/12\/05\/natural-order-sorting\/\">three times<\/a> in the past. It just goes to show how well-written his piece of code is.<\/p><p>This submission is something that I have needed to do numerous times in the past, which is creating unique, incrementing file name.<\/p><p>Let&#8217;s say that there are three files in the current folder: &#8220;A001.txt&#8221;, &#8220;A002.txt&#8221;, &#8220;A003.txt&#8221;. I want to determine the next file name to use. In this case, the file name will be &#8220;A004.txt&#8221;. And let&#8217;s say that I need to do this type of operation repeatedly. Of course, I can keep track of the file names, and increment accordingly. Instead, I would like to automatically determine the next possible file name, since the current folder may contain some files from previous runs.<\/p><p>With <tt>nextname<\/tt>, you specify the file name structure, including the number to start incrementing. It will automatically start counting up until it finds a file name that is not being used.<\/p><pre class=\"codeinput\">dir <span class=\"string\">A*.txt<\/span>\r\n<\/pre><pre class=\"codeoutput\">\r\nA001.txt  A002.txt  A003.txt  \r\n\r\n<\/pre><pre class=\"codeinput\">newname = nextname(<span class=\"string\">'A'<\/span>,<span class=\"string\">'001'<\/span>,<span class=\"string\">'.txt'<\/span>)\r\n<\/pre><pre class=\"codeoutput\">newname =\r\n    'A004.txt'\r\n<\/pre><p>The numbers could be in different formats, and you can have special characters.<\/p><pre class=\"codeinput\">dir <span class=\"string\">B*.txt<\/span>\r\n<\/pre><pre class=\"codeoutput\">\r\nB_01.txt  B_02.txt  B_05.txt  \r\n\r\n<\/pre><pre class=\"codeinput\">newname = nextname(<span class=\"string\">'B'<\/span>,<span class=\"string\">'_01'<\/span>,<span class=\"string\">'.txt'<\/span>)\r\n<\/pre><pre class=\"codeoutput\">newname =\r\n    'B_03.txt'\r\n<\/pre><p>Stephen includes an example with the File Exchange entry, and the function has well-written help. The code is elegently written, with use of <a href=\"https:\/\/www.mathworks.com\/help\/matlab\/matlab_prog\/regular-expressions.html\">regular expressions<\/a>.<\/p><p><b>Comments<\/b><\/p><p>Give it a try and let us know what you think <a href=\"http:\/\/blogs.mathworks.com\/pick\/?p=11275#respond\">here<\/a> or leave a <a href=\"https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/64108#comment\">comment<\/a> for Stephen.<\/p><script language=\"JavaScript\"> <!-- \r\n    function grabCode_579f1065c681496ea2cf39cce16dd165() {\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='579f1065c681496ea2cf39cce16dd165 ' + '##### ' + 'SOURCE BEGIN' + ' #####';\r\n        t2='##### ' + 'SOURCE END' + ' #####' + ' 579f1065c681496ea2cf39cce16dd165';\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        copyright = 'Copyright 2020 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 copyright line at the bottom if specified.\r\n        if (copyright.length > 0) {\r\n            d.writeln('');\r\n            d.writeln('%%');\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     --> <\/script><p style=\"text-align: right; font-size: xx-small; font-weight:lighter;   font-style: italic; color: gray\"><br><a href=\"javascript:grabCode_579f1065c681496ea2cf39cce16dd165()\"><span style=\"font-size: x-small;        font-style: italic;\">Get \r\n      the MATLAB code <noscript>(requires JavaScript)<\/noscript><\/span><\/a><br><br>\r\n      Published with MATLAB&reg; R2019b<br><\/p><p class=\"footer\"><br>\r\n      Published with MATLAB&reg; R2019b<br><\/p><\/div><!--\r\n579f1065c681496ea2cf39cce16dd165 ##### SOURCE BEGIN #####\r\n%%\r\n% <http:\/\/www.mathworks.com\/matlabcentral\/profile\/authors\/869871 Jiro>'s\r\n% Pick this week is\r\n% <https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/64108 |nextname|>\r\n% by <https:\/\/www.mathworks.com\/matlabcentral\/profile\/authors\/3102170\r\n% Stephen Cobeldick>.\r\n%\r\n% Stephen is no stranger in the MATLAB user community arena. As of the\r\n% writing of this post, Stephen ranks 44 out of over 15,000 File Exchange\r\n% users. He also ranks 5 out of over 190,000 MATLAB Answers users. One of\r\n% his files have been Picked not\r\n% <https:\/\/blogs.mathworks.com\/pick\/2015\/01\/09\/spelling-out-numbers\/ once>,\r\n% not\r\n% <https:\/\/blogs.mathworks.com\/pick\/2019\/04\/26\/convert-between-rgb-and-color-names\/\r\n% twice>, but\r\n% <https:\/\/blogs.mathworks.com\/pick\/2014\/12\/05\/natural-order-sorting\/ three\r\n% times> in the past. It just goes to show how well-written his piece of\r\n% code is.\r\n%\r\n% This submission is something that I have needed to do numerous times in\r\n% the past, which is creating unique, incrementing file name.\r\n%\r\n% Let's say that there are three files in the current folder: \"A001.txt\",\r\n% \"A002.txt\", \"A003.txt\". I want to determine the next file name to use. In\r\n% this case, the file name will be \"A004.txt\". And let's say that I need to\r\n% do this type of operation repeatedly. Of course, I can keep track of the\r\n% file names, and increment accordingly. Instead, I would like to\r\n% automatically determine the next possible file name, since the current\r\n% folder may contain some files from previous runs.\r\n%\r\n% With |nextname|, you specify the file name structure, including the\r\n% number to start incrementing. It will automatically start counting up\r\n% until it finds a file name that is not being used.\r\n\r\ndir A*.txt\r\n\r\n%%\r\nnewname = nextname('A','001','.txt')\r\n\r\n%%\r\n% The numbers could be in different formats, and you can have special\r\n% characters.\r\n\r\ndir B*.txt\r\n\r\n%%\r\nnewname = nextname('B','_01','.txt')\r\n\r\n%%\r\n% Stephen includes an example with the File Exchange entry, and the\r\n% function has well-written help. The code is elegently written, with use\r\n% of\r\n% <https:\/\/www.mathworks.com\/help\/matlab\/matlab_prog\/regular-expressions.html\r\n% regular expressions>.\r\n%\r\n% *Comments*\r\n%\r\n% Give it a try and let us know what you think\r\n% <http:\/\/blogs.mathworks.com\/pick\/?p=11275#respond here> or leave a\r\n% <https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/64108#comment\r\n% comment> for Stephen.\r\n\r\n##### SOURCE END ##### 579f1065c681496ea2cf39cce16dd165\r\n-->","protected":false},"excerpt":{"rendered":"<p>\r\nJiro&#8216;s Pick this week is nextname by Stephen Cobeldick.Stephen is no stranger in the MATLAB user community arena. As of the writing of this post, Stephen ranks 44 out of over 15,000 File&#8230; <a class=\"read-more\" href=\"https:\/\/blogs.mathworks.com\/pick\/2020\/01\/10\/determining-next-available-file-name\/\">read more >><\/a><\/p>","protected":false},"author":35,"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\/11275"}],"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\/35"}],"replies":[{"embeddable":true,"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/comments?post=11275"}],"version-history":[{"count":2,"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/posts\/11275\/revisions"}],"predecessor-version":[{"id":11279,"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/posts\/11275\/revisions\/11279"}],"wp:attachment":[{"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/media?parent=11275"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/categories?post=11275"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/tags?post=11275"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}