Method to determine whether given folder path is local (vs networked)?

44 ビュー (過去 30 日間)
Andrew Diamond
Andrew Diamond 2021 年 8 月 8 日
編集済み: Andrew Diamond 2021 年 8 月 8 日
Programatically given a string. This includes URL or Windows file or folder path strings.
Something like isfolder but islocalfolder or isnetworkedfolder

採用された回答

Walter Roberson
Walter Roberson 2021 年 8 月 8 日
You might possibly be able to use a .NET System.IO.DriveInfo
Although that is talking about C#, System.* is at least partially available in MATLAB, such as System.Diagnostics.Process https://www.mathworks.com/matlabcentral/answers/575608-system-and-system-diagnostics-process#answer_485447
  2 件のコメント
Andrew Diamond
Andrew Diamond 2021 年 8 月 8 日
編集済み: Andrew Diamond 2021 年 8 月 8 日
e.g.
isMappedNetworkedPath = strcmp(System.IO.DriveInfo(System.IO.Path.GetPathRoot('Y:\')).DriveType,'Network')
where Y here is a network mapped drive.
Thanks
==
Unfortunately, it doesn't handle UNC paths,\\<computername>\....., but then I thik they're generally easy to ID as they start with '\\' . Even so you can construct a UNC to a local path but then it's computer name should be equal to the env COMPUTERNAME variable, So:
ComputerUNC=fileparts(<UNCPath>)
% so ComputerUNC in the form \\<computername>
isNetworkUNC = ~strcmpi(ComputerUNC(3:end),getenv('COMPUTERNAME'))
I'm not sure if there's other UNC forms.
====
function [bIsNetworked, bIsUNC]=isnetworkpath(Path)
if(length(Path) < 3)
error('Path %s has to be at lest 3 characters', Path);
end
if(strcmp(Path(1:2), '\\')) % UNC Path
bIsUNC = true;
ComputerUNC=fileparts(Path);
bIsNetworked = ~strcmpi(ComputerUNC(3:end),getenv('COMPUTERNAME'));
else
bIsUNC = false;
bIsNetworked = strcmp(System.IO.DriveInfo(System.IO.Path.GetPathRoot(Path)).DriveType,'Network');
end
Walter Roberson
Walter Roberson 2021 年 8 月 8 日
There is the additional possibility that the computer name somehow resolves to the current computer. localhost or 127.0.0.1 or an ip address that is one of the local host ip addresses. Also watch out for \\?
https://stackoverflow.com/questions/2787203/unc-path-to-a-folder-on-my-local-computer

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

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

Community Treasure Hunt

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

Start Hunting!

Translated by