{"id":6065,"date":"2015-06-26T09:00:02","date_gmt":"2015-06-26T13:00:02","guid":{"rendered":"https:\/\/blogs.mathworks.com\/pick\/?p=6065"},"modified":"2015-06-25T08:55:13","modified_gmt":"2015-06-25T12:55:13","slug":"getting-short-path-names","status":"publish","type":"post","link":"https:\/\/blogs.mathworks.com\/pick\/2015\/06\/26\/getting-short-path-names\/","title":{"rendered":"Getting short path names"},"content":{"rendered":"\r\n<div class=\"content\"><p><a href=\"https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/authors\/15007\">Jiro<\/a>'s pick this week is <a href=\"https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/48950-short-path-name-on-windows--com-server-\">Short Path Name on Windows<\/a> by <a href=\"https:\/\/www.mathworks.com\/matlabcentral\/profile\/authors\/492531\">Jerome Briot<\/a>.<\/p><p>Thanks, Jerome! Your File Exchange entry helped me big time in my project. I was creating a program that managed a bunch of files, and some of the tasks required me to copy files from one network location to a new location. Sometimes, the new location would have a path name of 261 or more characters. In Windows API, 260 characters is the maximum number of characters allowed in a file path. I can overcome this issue if I map a drive to some path. For example,<\/p><pre>\\\\networklocation\\folder1\\folder2\\folder3\\thisfile.txt\r\n    --&gt;\r\nN:\\folder3\\thisfile.txt<\/pre><p>However, I needed to make this a bit more portable so that my team members can use it on their machines. Then I remembered that back in the day, DOS used to abbreviate long path\/file names (<a href=\"https:\/\/en.wikipedia.org\/wiki\/8.3_filename\">8.3 filename<\/a>) to deal with its character limit. After Windows&reg; 95, I've since slowly forgotten about the existence of 8.3 filename. But now, I have a real need for it.<\/p><p>I looked around the Web for the rules for converting a path name to a short path name. That's when I bumped into Jerome's entry. Rather than manually constructing the short file\/path names, Jerome's functions uses Windows FileSystemObject (COM) to get the short names. This is obviously much more robust and the correct way.<\/p><pre class=\"codeinput\">thisFile = which(mfilename)\r\nthisFile_short = fsoGetShortPath(thisFile)\r\n<\/pre><pre class=\"codeoutput\">thisFile =\r\nC:\\Users\\jdoke\\OneDrive for Business\\MyStuff\\Work\\POTW\\2015_06_26_ShortPathName\\potw_shortname.m\r\nthisFile_short =\r\nC:\\Users\\jdoke\\ONEDRI~1\\MyStuff\\Work\\POTW\\206E2D~1\\POTW_S~1.M\r\n<\/pre><p>Thanks again, Jerome. It worked perfectly!<\/p><p><b>Comments<\/b><\/p><p>Give this a try, let us know what you think <a href=\"https:\/\/blogs.mathworks.com\/pick\/?p=6065#respond\">here<\/a> or leave a <a href=\"https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/48950-short-path-name-on-windows--com-server-#comments\">comment<\/a> for Jerome.<\/p><script language=\"JavaScript\"> <!-- \r\n    function grabCode_d2c3f018f0a5453488f7dbd4d7c8b263() {\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='d2c3f018f0a5453488f7dbd4d7c8b263 ' + '##### ' + 'SOURCE BEGIN' + ' #####';\r\n        t2='##### ' + 'SOURCE END' + ' #####' + ' d2c3f018f0a5453488f7dbd4d7c8b263';\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 2015 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_d2c3f018f0a5453488f7dbd4d7c8b263()\"><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; R2015a<br><\/p><p class=\"footer\"><br>\r\n      Published with MATLAB&reg; R2015a<br><\/p><\/div><!--\r\nd2c3f018f0a5453488f7dbd4d7c8b263 ##### SOURCE BEGIN #####\r\n%%\r\n% <https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/authors\/15007\r\n% Jiro>'s pick this week is\r\n% <https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/48950-short-path-name-on-windows--com-server- Short Path\r\n% Name on Windows> by\r\n% <https:\/\/www.mathworks.com\/matlabcentral\/profile\/authors\/492531 Jerome\r\n% Briot>.\r\n%\r\n% Thanks, Jerome! Your File Exchange entry helped me big time in my\r\n% project. I was creating a program that managed a bunch of files, and some\r\n% of the tasks required me to copy files from one network location to\r\n% a new location. Sometimes, the new location would have a path name of 261\r\n% or more characters. In Windows API, 260 characters is the maximum number\r\n% of characters allowed in a file path. I can overcome this issue if I map\r\n% a drive to some path. For example,\r\n%\r\n%  \\\\networklocation\\folder1\\folder2\\folder3\\thisfile.txt\r\n%      REPLACE_WITH_DASH_DASH>\r\n%  N:\\folder3\\thisfile.txt\r\n%\r\n% However, I needed to make this a bit more portable so that my team\r\n% members can use it on their machines. Then I remembered that back in the\r\n% day, DOS used to abbreviate long path\/file names\r\n% (<https:\/\/en.wikipedia.org\/wiki\/8.3_filename 8.3 filename>) to deal with\r\n% its character limit. After Windows(R) 95, I've since slowly forgotten\r\n% about the existence of 8.3 filename. But now, I have a real need for it.\r\n%\r\n% I looked around the Web for the rules for converting a path name to a\r\n% short path name. That's when I bumped into Jerome's entry. Rather than\r\n% manually constructing the short file\/path names, Jerome's functions uses\r\n% Windows FileSystemObject (COM) to get the short names. This is obviously\r\n% much more robust and the correct way.\r\n\r\nthisFile = which(mfilename)\r\nthisFile_short = fsoGetShortPath(thisFile)\r\n\r\n%%\r\n% Thanks again, Jerome. It worked perfectly!\r\n%\r\n% *Comments*\r\n%\r\n% Give this a try, let us know what you think\r\n% <https:\/\/blogs.mathworks.com\/pick\/?p=6065#respond here> or leave a\r\n% <https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/48950-short-path-name-on-windows--com-server-#comments\r\n% comment> for Jerome.\r\n\r\n##### SOURCE END ##### d2c3f018f0a5453488f7dbd4d7c8b263\r\n-->","protected":false},"excerpt":{"rendered":"<p>\r\nJiro's pick this week is Short Path Name on Windows by Jerome Briot.Thanks, Jerome! Your File Exchange entry helped me big time in my project. I was creating a program that managed a bunch of... <a class=\"read-more\" href=\"https:\/\/blogs.mathworks.com\/pick\/2015\/06\/26\/getting-short-path-names\/\">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\/6065"}],"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=6065"}],"version-history":[{"count":2,"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/posts\/6065\/revisions"}],"predecessor-version":[{"id":6067,"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/posts\/6065\/revisions\/6067"}],"wp:attachment":[{"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/media?parent=6065"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/categories?post=6065"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/tags?post=6065"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}