Error using rmdir for deleting slprj folder

8 visualizzazioni (ultimi 30 giorni)
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\")

Risposta accettata

Image Analyst
Image Analyst il 20 Mag 2023
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

Più risposte (0)

Prodotti


Release

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by