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

4 ビュー (過去 30 日間)
cova buraz
cova buraz 2016 年 10 月 3 日
コメント済み: Guillaume 2016 年 10 月 22 日
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!

回答 (1 件)

Guillaume
Guillaume 2016 年 10 月 3 日
編集済み: Guillaume 2016 年 10 月 5 日
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 件のコメント
cova buraz
cova buraz 2016 年 10 月 20 日
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!!!
Guillaume
Guillaume 2016 年 10 月 22 日
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.

サインインしてコメントする。

カテゴリ

Help Center および File ExchangePerformance and Memory についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by