HumbleMan
HumbleMan

Reputation: 53

Remove folder if it contain less than two files

I have a lot of folders containing .fig files. Some of these folders contains more than one file which is what I want. The others that only contain one file should get removed with a script.

I thought I could (somehow, I am totally fresh) iterate trough the folders (which exists in one folder will all this other folders) and check if the dir contains more than one file, if not: rmdir(folderName).

Is this possible? Help is very appreciated!

Upvotes: 0

Views: 61

Answers (1)

fcdimitr
fcdimitr

Reputation: 528

Yes, this is possible through MATLAB

directoryName = 'folderName';
contents = dir(directoryName)
if length(contents) <= 1
    rmdir(directoryName);
end

You can also iterate through multiple directories with

files = dir('./');

dirFlags = [files.isdir];

subFolders = files(dirFlags);

for k = 1:length(subFolders)
    directoryName = subFolders(k).name;
    contents = dir(directoryName);
    if length(contents) <= 1
        rmdir(directoryName);
    end
end

You should probably check that the subFolder is not . or ..

Upvotes: 2

Related Questions