Error using rmdir for deleting slprj folder

조회 수: 8 (최근 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 CenterFile Exchange에서 Startup and Shutdown에 대해 자세히 알아보기

제품


릴리스

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by