Matlab Script-Moving of multiple files from multiple folders into specific folders
4 views (last 30 days)
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!





0 Comments
Answers (1)
Guillaume
on 3 Oct 2016
Edited: Guillaume
on 5 Oct 2016
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
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.
See Also
Categories
Find more on Performance and Memory 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!