フィルターのクリア

How to get drive name?

23 ビュー (過去 30 日間)
Ethan Goldstein
Ethan Goldstein 2020 年 5 月 8 日
編集済み: Eivind Hennestad 2022 年 11 月 25 日
I am trying to get the drive name, not just the drive letter. For example, I have my 'C' drive and it is called 'OSDisk'. Given I know the drive I want to find the name of ('C'), what function should I call to get the name 'OSDisk'?

採用された回答

per isakson
per isakson 2020 年 5 月 8 日
編集済み: per isakson 2020 年 5 月 8 日
On Windows this function works with my local drives
>> DriveName( 'C' )
ans =
'OSDisk'
>> DriveName( 'D' )
ans =
'DATA'
>>
where
function drive_name = DriveName( drive_letter )
cmd_str = sprintf( 'dir %s:\\zzzzzz', drive_letter );
[~,msg] = system( cmd_str );
cac = strsplit( msg, '\n' );
has = contains( cac, 'Volume in drive');
drive_name = regexp( cac{has}, '(?<= is ).+$', 'match', 'once' );
end
I'm sure there are more robust solutions, see e.g. GetVolumeInformationA function
A bit better
function drive_name = DriveName( drive_letter )
cmd_str = sprintf( 'vol %s:', drive_letter );
[~,msg] = system( cmd_str );
cac = strsplit( msg, '\n' );
drive_name = regexp( cac{1}, '(?<= is ).+$', 'match', 'once' );
end
  3 件のコメント
per isakson
per isakson 2020 年 5 月 8 日
This is the most universal I can think of
function drive_name = DriveName( drive_letter )
if ismac
% Code to run on Mac platform
elseif isunix
% Code to run on Linux platform
elseif ispc
% Code to run on Windows platform
else
disp('Platform not supported')
end
end
Ethan Goldstein
Ethan Goldstein 2020 年 5 月 8 日
Thank You!

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

その他の回答 (1 件)

Eivind Hennestad
Eivind Hennestad 2022 年 11 月 25 日
編集済み: Eivind Hennestad 2022 年 11 月 25 日
An alternative is to use listPhysicalDrives from file exchange

カテゴリ

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

Community Treasure Hunt

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

Start Hunting!

Translated by