File Exchange Pick of the Week

Our best user submissions

Getting short path names

Jiro'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 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,

\\networklocation\folder1\folder2\folder3\thisfile.txt
    -->
N:\folder3\thisfile.txt

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 (8.3 filename) to deal with its character limit. After Windows® 95, I've since slowly forgotten about the existence of 8.3 filename. But now, I have a real need for it.

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.

thisFile = which(mfilename)
thisFile_short = fsoGetShortPath(thisFile)
thisFile =
C:\Users\jdoke\OneDrive for Business\MyStuff\Work\POTW\2015_06_26_ShortPathName\potw_shortname.m
thisFile_short =
C:\Users\jdoke\ONEDRI~1\MyStuff\Work\POTW\206E2D~1\POTW_S~1.M

Thanks again, Jerome. It worked perfectly!

Comments

Give this a try, let us know what you think here or leave a comment for Jerome.




Published with MATLAB® R2015a

|

Comments

To leave a comment, please click here to sign in to your MathWorks Account or create a new one.