Skip to content

Commit 9746145

Browse files
committed
add Windows short name using COM server
1 parent 6daca45 commit 9746145

File tree

3 files changed

+53
-1
lines changed

3 files changed

+53
-1
lines changed

+stdlib/+fileio/windows_shortname.m

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
function short = windows_shortname(p)
2+
%% windows_shortname Retrieves the Windows short name (8.3 character) form
3+
%
4+
% Example of using a COM server (Scripting.FileSystemObject) in Windows
5+
%
6+
% References:
7+
% https://learn.microsoft.com/en-us/previous-versions/windows/internet-explorer/ie-developer/windows-scripting/ch28h2s7
8+
% https://www.mathworks.com/matlabcentral/fileexchange/48950-short-path-name-on-windows-com-server
9+
%
10+
11+
12+
arguments
13+
p (1,1) string
14+
end
15+
16+
assert(ispc, 'Only available on Windows')
17+
18+
fso = actxserver('Scripting.FileSystemObject');
19+
20+
if isfolder(p)
21+
short = fso.GetFolder(p).ShortPath;
22+
elseif isfile(p)
23+
short = fso.GetFile(p).ShortPath;
24+
else
25+
error('Path "%s" not found', p);
26+
end
27+
28+
delete(fso);
29+
30+
end

+stdlib/+sys/subprocess_run.m

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030
[status, msg] = system(cmd);
3131
else
3232
envCell = namedargs2cell(opt.env);
33-
33+
% https://www.mathworks.com/help/matlab/ref/system.html
3434
[status, msg] = system(cmd, envCell{:});
3535
end
3636
if ~isempty(opt.cwd)

+stdlib/+test/TestWindowsCOM.m

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
classdef TestWindowsCOM < matlab.unittest.TestCase
2+
3+
methods (Test)
4+
5+
function test_shortname(tc)
6+
7+
tc.assumeTrue(ispc, "Windows only")
8+
9+
test_file = fullfile(getenv("LocalAppData"), 'Microsoft\WindowsApps\notepad.exe');
10+
tc.assumeTrue(isfile(test_file))
11+
12+
short = stdlib.fileio.windows_shortname(test_file);
13+
14+
tc.verifyTrue(endsWith(short, 'notepad.exe'), "Short name should end with 'notepad.exe'")
15+
tc.verifyTrue(contains(short, "MICROS~1"))
16+
17+
end
18+
19+
end
20+
21+
end
22+

0 commit comments

Comments
 (0)