how to get UNC path for mapped drive path

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.

Antworten (1)

Prof. X
Prof. X am 21 Feb. 2022
Bearbeitet: Prof. X am 21 Feb. 2022

1 Stimme

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.

2 Kommentare

Wolfie
Wolfie am 26 Sep. 2025
Bearbeitet: Wolfie am 26 Sep. 2025
Taking inspiration from this, it could be sortened by using the 2nd output of system instead of a temp file, and then using regexp to extract the line after the desired drive letter
Your comment about removing additional stuff like "Windows Mapped Drive" or "Microsoft Windows Network" still applies.
driveLetter = 'X';
[~,netuse] = system( 'net use' );
mappedPath = strtrim( regexp( netuse, ['(?<=' driveLetter ':).+?^'], 'match', 'once', 'lineanchors' ) );
Prof. X
Prof. X am 11 Nov. 2025
Nice! I've never seen lineanchors used

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu Filename Construction finden Sie in Hilfe-Center und File Exchange

Gefragt:

am 3 Mai 2018

Kommentiert:

am 11 Nov. 2025

Community Treasure Hunt

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

Start Hunting!

Translated by