Matlab Script-Moving of multiple files from multiple folders into specific folders

Hallo, i need to write a script, wich moves multiple files from multiple folders into specific folders. There are 18 folders (shown in 1.jpg). Each of them contains two subfolders, as shown in 2.jpg. There are 80 files in Body 0.5 CE and 37 files in Body 1.5 CE (shown in 3.jpg and 4.jpg). The script should move 80 files (Body 0.5 CE) from each folder (140224.300, 140225.700, ...,140227.700) to complementary folder of t01, t02, ...t18 structure.
For example >> C:\Users\dan\Desktop\dicom1\sort\140224.300\Body 0.5 CE_Series0006...move to...C:\Users\dan\Desktop\PilotStudiexx_new_template\2-DynCT\0.5mm\t01. And so on, until content of all 18 folders is moved to t01-t18. After that i will use the same method for moving the Body 1.5 CE files. The script should be portable.
I hope i've expressed myself clearly. Any suggestion would be very helpfull!
Thank you!

Answers (1)

If the destination folders don't already exist, then you could just rename the original folders. Assuming, the destination already exist, here is a rough, untested (there may be bugs/typos), outline of how I would do it:
srcroot = 'C:\Users\dan\Desktop\dicom1\sort\';
destroot = 'C:\Users\dan\Desktop\PilotStudiexx_new_template\2-DynCT\0.5mm';
rootdir = dir(srcroot); %get a list of all files and folders
srcfolders = {rootdir.name}; %put the names into cell array
srcfolders = srcfolders([rootdir.isdir] & ~ismember(srcfolders, {'.', '..'})); %only keep directories but not the '.' and '..' that matlab stupidly return.
for fldidx = 1:numel(srcfolders)
destfolder = fullfile(destroot, sprintf('t%02d', fldidx));
movefile(fullfile(srcroot, srcfolders{fldidx}, '*'), destfolder);
end

7 Comments

Thanx for your code. I've tried it out, but it doesn't work properly. It moves all folders from root directory to one (first) destination folder, and the loop stops than. It should move first folder of root directory to first destination folder, the second from root to second destination and so on. I think it has to be one more FOR loop embedded in code. Any idea?
@cova buraz: Could you try to modify the code by you own to adjust it to you needs?
As said, the code may have some minor bugs but you should get the idea of how to do what you want from it nonetheless.
Saying that, I've made some edits which should fix the bugs.
The code works properly, but only for moving all subfolders together to destination folder. It needs to move first subfolder (Body 0.5 CE) to first destination and second subfolder(Body 1.5 CE) to second destination path.It doesnt work properly.Could you give me a hint what am i making wrong. I'm new in matlab. Here is my code:
srcroot = 'C:\Users\User\Documents\lbi_patient_study\LBI_PilotStudie22';
destroot1 = 'C:\Users\User\Documents\lbi_patient_study\LBI_PilotStudiexx_new_template\2-DynCT\0.5mm';
destroot2 = 'C:\Users\User\Documents\lbi_patient_study\LBI_PilotStudiexx_new_template\2-DynCT\1.5mm';
rootdir = dir(srcroot); %get a list of all files and folders
srcfolders = {rootdir.name}; %put the names into cell array
srcfolders = srcfolders([rootdir.isdir] & ~ismember(srcfolders, {'.', '..'})); %only keep directories but not the '.' and '..' that matlab stupidly return.
for fldidx = 1:numel(srcfolders)-2
subfolderroot=fullfile(srcroot, srcfolders{fldidx}, '*' ); %recursive subfolderpath
subfolderdir=dir(subfolderroot); %get a list of all files and subfoldersfolders
subfolderlist = {subfolderdir.name}; %put the names into cell array
subfolderlist = subfolderlist([subfolderdir.isdir] & ~ismember(subfolderlist, {'.', '..'}));
destfolder1 = fullfile(destroot1, sprintf('t%02d', fldidx));
movefile(fullfile(srcroot, srcfolders{fldidx}, subfolderlist{1}, '*'), destfolder1);
destfolder2 = fullfile(destroot2, sprintf('t%02d', fldidx));
movefile(fullfile(srcroot, srcfolders{fldidx}, subfolderlist{2}, '*'), destfolder2);
% movefile(fullfile(srcroot, srcfolders{fldidx}, '*'), destfolder1);
end
Important: You should not subtract 2 from the folder count, otherwise you'll be missing two folders. You will find some code where people skip the first two folders returned by dir (but never the last two as you did here) because they assume that they are the '.' and '..' folders. This is usually the case but not always. My approach is a lot safer, it explicitly removes the folders by name BEFORE the loop.
If I understood correctly, this should do what you want:
%Move files from srcroot\N\Body %f CE_*\*
%to destroot\%fmm\NN\*
%where NN is a number replacing the original N folder
srcroot = 'C:\Users\User\Documents\lbi_patient_study\LBI_PilotStudie22';
destroot = 'C:\Users\User\Documents\lbi_patient_study\LBI_PilotStudiexx_new_template\2-DynCT\';
rootdir = dir(srcroot); %get a list of all files and folders
srcfolders = {rootdir.name}; %put the names into cell array
srcfolders = srcfolders([rootdir.isdir] & ~ismember(srcfolders, {'.', '..'})); %only keep directories but not the '.' and '..' that matlab stupidly return.
%at this point srcfolders is the list of the N folders
for fldidx = 1:numel(srcfolders)
subfolderroot=fullfile(srcroot, srcfolders{fldidx)); %recursive subfolderpath
subfolderdir = dir(subfolderroot);
subfldfolders = {subfolderdir.name};
subfldfolders = subfldfolders([subfldfolders.isdir] & ~ismember(subfldfolders, {'.', '..'}));
for subfldidx = 1:numel(subfldfolders)
%iterating over the BODY %f CE_xxx folders
scandistance = str2double(regexp(subfldfolders{subfldidx}, '(?<=BODY ).*(?= CE)', 'match', 'once'));
destfolder = fullfile(destroot, sprintf('%g', scandistance), sprintf('t%02d', fldidx));
movefile(fullfile(subfolderroot, subfldfolders{subfldidx}, '*'), destfolder);
end
end
Again, completely untested. There may be bugs/typos/misunderstandings.
I substracted 2 from the folder count because i dont need last 2 folders. Your code moves content of both subfolders (Body 0.5 CE and Body 1.500 CE) to one destination folder and creates also a new destination folder. It needs to move content of Body 0.5 CE to destination folder 0.5mm/t01 and content of Body 1.500 CE to folder 1.5mm/t01 and so on... Example:
First iteration Move content of C:\Users\User\Documents\lbi_patient_study\LBI_PilotStudie22 - Copy\ 134801.450\Body 0.5 CE_Series0005 to C:\Users\User\Documents\lbi_patient_study\LBI_PilotStudiexx_new_template\ 2-DynCT\0.5mm\t01
Move content of C:\Users\User\Documents\lbi_patient_study\LBI_PilotStudie22 - Copy\ 134801.450\Body 1.500 CE_Series0013 to C:\Users\User\Documents\lbi_patient_study\LBI_PilotStudiexx_new_template\ 2-DynCT\1.5mm\t01
Second iteration Move content of C:\Users\User\Documents\lbi_patient_study\LBI_PilotStudie22 - Copy\ 134802.800\Body 0.5 CE_Series0005 to C:\Users\User\Documents\lbi_patient_study\LBI_PilotStudiexx_new_template\ 2-DynCT\0.5mm\t02
Move content of C:\Users\User\Documents\lbi_patient_study\LBI_PilotStudie22 - Copy\ 134802.800\Body 1.500 CE_Series0014 to C:\Users\User\Documents\lbi_patient_study\LBI_PilotStudiexx_new_template\ 2-DynCT\1.5mm\t02
and so on...
This part of code:
scandistance = str2double(regexp(subfldfolders{subfldidx}, '(?<=BODY ).*(?= CE)', 'match', 'once'));
destfolder = fullfile(destroot, sprintf('%g', scandistance), sprintf('t%02d', fldidx));
creates additionaly destination folders t01, t02...t24 and i have already created those destination folders.
Wich token should i use for: Body 0.5 CE_? Is it enough to write '(0.5)\w+' or '(?<=BODY ).*(?= 0.5)' when i want to match Body 0.5 CE_ strings?
Thank you for your help!!!
Well, the intent of my latest code was to create the exact same structure that you describe, so if it doesn't it's because I've overlooked something. Rereading the code I can't see what it would be. The only missing thing is the 'mm' suffix to your directories, which is easily fixed, replace the sprintf('%g', scandistance) by:
sprintf('%gmm', scantdistance)
If the destination folders already exist, it should just fill them up with the moved content.

Sign in to comment.

Categories

Asked:

on 3 Oct 2016

Commented:

on 22 Oct 2016

Community Treasure Hunt

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

Start Hunting!