Matlab Script-Moving of multiple files from multiple folders into specific folders
Show older comments
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
cova buraz
on 5 Oct 2016
Jan
on 5 Oct 2016
@cova buraz: Could you try to modify the code by you own to adjust it to you needs?
Guillaume
on 5 Oct 2016
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.
cova buraz
on 17 Oct 2016
Guillaume
on 17 Oct 2016
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.
cova buraz
on 20 Oct 2016
Guillaume
on 22 Oct 2016
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.
Categories
Find more on File Operations in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!