|
| 1 | +%% SYS.IS_REMOVABLE |
| 2 | + |
1 | 3 | function y = is_removable(filepath) |
2 | 4 |
|
3 | 5 | y = false; |
4 | 6 |
|
5 | 7 | if ispc() |
6 | | - drive = stdlib.root_name(filepath); |
7 | | - cmd1 = sprintf('wmic logicaldisk where "DeviceID=''%s''" get DriveType', drive); |
| 8 | + r = stdlib.root_name(filepath); |
| 9 | + if ~strlength(r), return, end |
| 10 | + r = extractBefore(r, 2); |
| 11 | + |
| 12 | + psFile = fullfile(fileparts(mfilename('fullpath')), "isRemovableDrive.ps1"); |
| 13 | + mustBeFile(psFile) |
| 14 | + |
| 15 | + psCmd = sprintf(". '%s'; IsRemovableDrive -DriveLetter '%s'", psFile, r); |
| 16 | + |
| 17 | + cmd1 = sprintf('powershell -ExecutionPolicy Bypass -Command "& {%s}"', psCmd); |
8 | 18 | else |
9 | 19 | cmd1 = sprintf('df "%s" | tail -n 1 | awk ''{print $1}''', filepath); |
10 | 20 | end |
|
17 | 27 |
|
18 | 28 | if ispc() |
19 | 29 |
|
20 | | - y = any(ismember(strip(extractAfter(m1, "DriveType")), ["2", "5"])); |
21 | | - % https://learn.microsoft.com/en-us/windows/win32/cimwin32prov/win32-logicaldisk |
| 30 | + y = contains(m1, "True"); |
22 | 31 |
|
23 | 32 | elseif ismac() |
24 | 33 |
|
|
37 | 46 | end |
38 | 47 |
|
39 | 48 | end |
| 49 | + |
| 50 | +% We use Powershell .ps1 function because: |
| 51 | +% |
| 52 | +% drive = stdlib.root_name(filepath); |
| 53 | +% WMIC is not available on all systems e.g. GA windows-2025 runner image |
| 54 | +% WMIC doesn't detect USB flash drives -- it sees them as Fixed drives |
| 55 | +% cmd1 = sprintf('wmic logicaldisk where "DeviceID=''%s''" get DriveType', drive); |
| 56 | +% |
| 57 | +% (Get-Volume -DriveLetter H).DriveType also detects USB thumb drives as Fixed like HDD |
| 58 | +% |
| 59 | +% Get-WmiObject also sees USB as type 3 fixed disk |
| 60 | +% cmd1 = sprintf('pwsh -c "Get-WmiObject Win32_LogicalDisk -Filter ''DeviceID=''%s'''' | Select-Object -ExpandProperty DriveType"', drive); |
| 61 | + |
| 62 | + |
| 63 | + % WMIC: y = any(ismember(strip(extractAfter(m1, "DriveType")), ["2", "5"])); |
| 64 | + % https://learn.microsoft.com/en-us/windows/win32/cimwin32prov/win32-logicaldisk |
0 commit comments