how to get UNC path for mapped drive path

18 ビュー (過去 30 日間)
Albert Passy
Albert Passy 2018 年 5 月 3 日
編集済み: Prof. X 2022 年 2 月 21 日
I've got x:\path\filename.ext. I want to resolve this to \\MyUnc\Network\path\filename.ext. Any thoughts on how to do this programatically? Thanks.

回答 (1 件)

Prof. X
Prof. X 2022 年 2 月 21 日
編集済み: Prof. X 2022 年 2 月 21 日
If on Windows, you can use system('net use'). I use the following function in one of my codes. I'm sure there is a more efficient way to code this but this works for me.
function MappedPath = findMappedDrive(DriveLetter)
% DriveLetter is just the letter that represents the drive without a
% colon, type char.
DriveLetter = [DriveLetter ':'];
system('net use >netuseRun.txt')
netuseOpen = fopen('netuseRun.txt');
tScan = textscan(netuseOpen,'%s','Delimiter','\n');
fclose(netuseOpen);
delete('netuseRun.txt');
str = string(tScan{:});
locate = contains(str,DriveLetter);
locatemd = strfind(str(locate),'\\');
conv = char(str(locate));
MappedPath = strtrim(conv(locatemd:end));
end
Sometimes there may be stuff that gets added at the end of the MappedPath like 'Windows Mapped Drive' so you may want to make a check using exist(MappedPath,'dir'). Hope this helps.

カテゴリ

Help Center および File ExchangeMATLAB についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by