How can I get the full name of my folder when given a short name (8.3 name) in MATLAB R2019a on Windows?

7 views (last 30 days)
I have a short name and I like to get the full name so I can display it in a nice format on my GUI. How can I expand folders like 'C:\PROGRA~2\MIE74D~1'; to its readable format?

Accepted Answer

MathWorks Support Team
MathWorks Support Team on 3 Sep 2019
MATLAB has no built-in functionality for that. However, there are several Windows APIs you can use from MATLAB to get the fullname. One way could be ActiveX like shown below. This should work as long you do not hit the MAX_PATH length limitation on Windows:
name = 'C:\PROGRA~2\MIE74D~1';
fso = actxserver('Scripting.FileSystemObject');
if fso.FileExists(name)
f = fso.GetFile(name);
elseif fso.FolderExists(name)
f = fso.GetFolder(name);
end
app = actxserver('Shell.Application');
fullname = get(invoke(app.NameSpace(f.ParentFolder.Path),'ParseName',f.Name),'Path')
fso.release
app.release
Alternatively , if you have access to PowerShell you can use
name = 'C:\PROGRA~2\MIE74D~1';
[a,b] = system(sprintf('powershell.exe -Command "(Get-Item ''%s'').FullName"',name))
Or the Windows API function GetLongPathNameW
used through a MEX file (callled toFullPath.cpp)
#include "mex.h"
#include "windows.h"
#define MAX_SIZE 2000
void mexFunction(int nlhs, mxArray* plhs[], int nrhs, const mxArray* prhs[]) {
wchar_t lpszLongPath[MAX_SIZE];
wchar_t *tmp;
DWORD res;
mwSize dim[] = {1,0};
tmp = (wchar_t*) malloc(sizeof(wchar_t) * (mxGetNumberOfElements(prhs[0])+1) );
if (tmp == NULL) {
plhs[0] = mxCreateCharArray((mwSize)2,dim);
return;
}
wcsncpy(tmp, (wchar_t*)mxGetData(prhs[0]), mxGetNumberOfElements(prhs[0]));
tmp[mxGetNumberOfElements(prhs[0])] = L'\0';
res = GetLongPathNameW(tmp, lpszLongPath, MAX_SIZE);
free(tmp);
if ( res == 0){
plhs[0] = mxCreateCharArray((mwSize)2,dim);
return;
}
dim[1] = (mwSize)res;
plhs[0] = mxCreateCharArray((mwSize)2,dim);
wcsncpy((wchar_t*)mxGetData(plhs[0]),&(lpszLongPath[0]),res);
}
compiled with
mex toFullPath.cpp
and called through:
name = 'C:\PROGRA~2\MIE74D~1';
res = toFullPath(sprintf('\\\\?\\%s',name))
Note that a \\?\ was added in front of the path to indicate a long path for Windows to avoid the MAX_PATH length limitation as mentioned in the Microsoft documentation linked above. You would need to remove that prefix in the result afterwards.

More Answers (0)

Categories

Find more on Write C Functions Callable from MATLAB (MEX Files) in Help Center and File Exchange

Tags

No tags entered yet.

Products


Release

R2015b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!