Error using rmdir for deleting slprj folder

7 ビュー (過去 30 日間)
Tony Castillo
Tony Castillo 2023 年 5 月 20 日
回答済み: Image Analyst 2023 年 5 月 20 日
I would like to know why this rmdir comand does not work properly, this is the script made for deleting some variables and also the folder.
% % % %
current_path = pwd;
% %
%;
for k=1:64
FolderName=['CMkb1_',num2str(k)];
ruta=cd([current_path,'\',FolderName]);
delete Ib.mat
delete Ic.mat
delete IL.mat
delete Is.mat
delete kb1.mat
rmdir(ruta, "slprj\")
end
Error using rmdir
'slprj\' is an invalid option.
Error in Eliminar_mat_enSerie (line 17)
rmdir(ruta, "slprj\")

採用された回答

Image Analyst
Image Analyst 2023 年 5 月 20 日
You cannot delete a folder if somebody is using it or looking at it, for example if you cd'd to that folder in MATLAB, have it open in File Explorer, have some document living there open in some program such as Excel, etc.
Try this more robust, but untested, code:
currentFolder = pwd;
% Find out how many subfolders there are in the folder.
filePattern = fullfile(currentFolder, 'CMkb1*');
fileList = dir(filePattern);
numFolders = numel(fileList)
if numFolders == 0
warningMessage = sprintf('No CMkb1* subfolders found under "%s"', currentFolder);
uiwait(warndlg(warningMessage));
else
% Delete files in subfolders, then the subfolder itself.
for k = 1 : numFolders
thisBaseFolderName = ['CMkb1_', num2str(k)];
thisFullFolderName = fullfile(currentFolder, thisBaseFolderName);
if ~isfolder(thisFullFolderName)
% Skip this one if the folder does not exist.
fprintf('Folder "%s" not found.\n', thisFullFolderName);
continue;
end
% Delete the 5 .mat files.
fileToDelete = fullfile(thisBaseFolderName, 'ib.mat');
if isfile(fileToDelete)
delete(fileToDelete)
end
fileToDelete = fullfile(thisBaseFolderName, 'Ic.mat');
if isfile(fileToDelete)
delete(fileToDelete)
end
fileToDelete = fullfile(thisBaseFolderName, 'IL.mat');
if isfile(fileToDelete)
delete(fileToDelete)
end
fileToDelete = fullfile(thisBaseFolderName, 'Is.mat');
if isfile(fileToDelete)
delete(fileToDelete)
end
fileToDelete = fullfile(thisBaseFolderName, 'kb1.mat');
if isfile(fileToDelete)
delete(fileToDelete)
end
% You could also use a loop to delete all .mat files, or just use a wildcard to delete them all in one shot.
% Now remove the folder itself. I think the folder must be totally empty.
try
rmdir(thisFullFolderName)
fprintf('Folder "%s" removed.\n', thisFullFolderName);
catch ME
% Keep going even if this didn't work for this one folder.
end
end
end

その他の回答 (0 件)

カテゴリ

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

製品


リリース

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by