SYS.IS_REMOVABLE
function y = is_removable(filepath)
arguments
filepath (1,1) string
end
y = false;
if ispc()
r = stdlib.root_name(filepath);
if stdlib.strempty(r)
return
end
r = extractBefore(r, 2);
psFile = fullfile(fileparts(mfilename('fullpath')), "isRemovableDrive.ps1");
mustBeFile(psFile)
psCmd = sprintf('. ''%s''; IsRemovableDrive -DriveLetter ''%s''', psFile, r);
cmd = sprintf('powershell -ExecutionPolicy Bypass -Command "& {%s}"', psCmd);
else
cmd = sprintf('df "%s" | tail -n 1 | awk ''{print $1}''', filepath);
end
[s1, m1] = system(cmd);
if s1 ~= 0
warning('stdlib:is_removable:OSError', '%s', m1)
return
end
if ispc()
y = contains(m1, "True");
elseif ismac()
cmd2 = sprintf('diskutil info "%s"', m1);
[s2, m2] = system(cmd2);
y = s2 == 0 && contains(m2, "Removable Media:" + whitespacePattern + "Removable");
else
dev = strip(extractAfter(m1, '/dev/'));
f1 = sprintf('/sys/class/block/%s/removable', dev);
if isfile(f1)
y = strip(fileread(f1)) == "1";
end
end
end