TimeIsNear
TimeIsNear

Reputation: 755

Matlab - How to delete only contents from folder

I wanted to know how it is possible to delete only the contents (files and folders) of a specific folder, without deleting this folder. Delete only child folders. I have tried this, but this will delete the folder too:

rmdir(listFolders,'s')

Upvotes: 1

Views: 1894

Answers (1)

TimeIsNear
TimeIsNear

Reputation: 755

This function work fine:

function rmSubDir( pathDir )

    d = dir(pathDir);
    isub = [d(:).isdir];
    nameFolds = {d(isub).name}';
    nameFolds(ismember(nameFolds,{'.','..'})) = [];

     for i=1:size(nameFolds,1)
        dir2rm = fullfile(pathDir,nameFolds{i});
        rmdir(dir2rm, 's');
     end

end

Upvotes: 1

Related Questions