sorting folder based on what type of files it contains

1 view (last 30 days)
I want to sort folders based on what it contains. so i have folders with names 0008,0009, etc. they each have contents inside, but some does not have a .dcm file some does not have a bmp file ( inside subfolders ) . i want to sort this folders and put it into another parent folder no dicom and no bmp. i have attached a picture of how the folders i want to move look. I made the code below but it moved all the files to the no bmp folder instead
nufolderpath = '/Users/sesiliamaidelin/Downloads/summer project/Copy_of_nufolder';
nu_filesAndFolders = dir([nufolderpath '/**']);% Get all subfolder and files names
nu_folders = dir([nufolderpath])
nu_allfolders = {nu_folders.name}
nu_alllist={nu_filesAndFolders.name} % Convert to cell
for ll= 1:numel(nu_allfolders) %ok
currfolder = nu_allfolders{ll};
nupath = fullfile( nufolderpath, currfolder)
if isempty(dir(fullfile(nupath,'*.dcm')))
copyfile(fullfile(nufolderpath,currfolder), fullfile(nufolderpath, 'no dcm'))
end
end

Accepted Answer

Shravan Kumar Vankaramoni
Shravan Kumar Vankaramoni on 29 Jul 2021
Hi,
Below code demostrates sorting folders based on certain files and move them to another folder.
fileList = dir('*.bmp'); % It lists the files with .bmp extension
x = size(fileList); %Find the size of output
if(x(1) == 0) % if no .bmp files are present, move folder to nobmp folder
movefile source destination; %replace source and destination with respective paths
end
Refer the below link for more information:

More Answers (0)

Categories

Find more on File Operations in Help Center and File Exchange

Products


Release

R2021a

Community Treasure Hunt

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

Start Hunting!