MATLAB Answers

Dung Vu
0

loop over branch of subfolders to copy files, without pattern in file name, in folders with specific name scheme

Asked by Dung Vu
on 15 Nov 2019 at 13:28
Latest activity Commented on by meghannmarie on 21 Nov 2019 at 15:20
Hi everyone,
this will be a complex task.
I have following structure of folder and subfolders: starting with "movie":
Timepoint_1 many tif-files ...
2019_1 3245 Timepoint_2 *.tif
Timepoint_3 *.tif
Timepoint_4 tif-files ...
movie 2019_2 3245 Timepoint_5 *.tif
Timepoint_6 *.tif
Timepoint_7 tif-files ...
2019_3 3245 Timepoint_8 *.tif
Timepoint_9 *.tif
In all Timepoint_x folders are many tif-files. I want to:
  1. loop over all folders and subfolders to find all tif-files
  2. copy to another folder and
  3. rename them by
  4. sorting them in new folders with specific name scheme
How can I loop over all folders and subfolders till the function finds the tif files in a smart way? There are many existing functions to loop over subfolders. But I neither undertand nor even use them.
How can I combine the missing task with my existing code?
aim:
3_2_1_RFP_1.tif
3_2_1 3_2_1_RFP_10.tif
3_2_1_CFP_13.tif
image_sorted
3_2_1_RFP_1.tif
3_2_2 3_2_2_RFP_2.tif
3_2_2_YFP_2.tif
% create folder for sorted images = dest
outputFolder = fullfile(pwd, 'image_sorted')
if ~exist(outputFolder, 'dir')
mkdir(outputFolder);
end
%
path1 ='C:\Users\Desktop\rename_microscope_files\movie\2019_1\3245\TimePoint_1'
dest = 'C:\Users\Desktop\rename_microscope_files\image_sorted'
source =fullfile(path1,'*.tif');
myfile= dir(source);
% to do: 1. loop over all folders, number of timePoint_x folders can vary
% 2. loop over files, copy all tif files without thumb in filename to folder B
for i = 1:numel(myfile)
if ~contains(myfile(i).name, '_thumb')
copyfile(fullfile(path1, myfile(i).name), dest);
end
end
% to do: 3. rename files, assign file format
% e.g. tif file name in folder timePoint_1: movie_C02_w1_B9D7398B.tif
% --> 3_2_1_RFP_1.tif
% (row_column_site_wave_timepoint)
% this file should be put in folder 3_2_1
for i = 1:size(file_names)
fname = file_names(i).name;
fname = strsplit(fname,'_');
fname = char(fname);
row = fname(1); % takes 1. letter
col = str2num(fname(2:3)); % 2. and 3. letter
switch row
case 'A'
row = 1;
case 'B'
row = 2;
case 'C'
row = 3;
case 'D'
row = 4;
case 'E'
row = 5;
case 'F'
row = 6;
case 'G'
row = 7;
case 'H'
row = 8;
end
switch wave
case 'w1'
wave = RFP;
case 'w2'
wave = YFP;
case 'w3'
wave = CFP;
end
% build structure .row = defined features
new_fname_info(i).row = row;
new_fname_info(i).col = col;
new_fname_info(i).site = 1;
new_fname_info(i).org_fname = file_names(i).name;
new_fname_info.channel = wave;
new_fname_info.timepoint = -1; % space holder, return struct
end
% to do: 4. to do: sorting files and moving into parent folder like 3_2_1

  0 Comments

Sign in to comment.

2 Answers

Answer by meghannmarie on 15 Nov 2019 at 14:18
Edited by meghannmarie on 18 Nov 2019 at 18:20

Are you trying to do something like this?
outputFolder = fullfile(pwd, 'image_sorted')
if ~exist(outputFolder, 'dir')
mkdir(outputFolder);
end
path1 ='C:\Users\Desktop\rename_microscope_files\movie\2019_1\3245\TimePoint_1'
dest = 'C:\Users\Desktop\rename_microscope_files\image_sorted'
% to do: 1. loop over all folders, number of timePoint_x folders can vary
source =fullfile(path1,'**\*.tif');
myfiles = dir(source);
% 2. loop over files, copy all tif files without thumb in filename to folder B
for i = 1:numel(myfiles)
myfile = myfiles(i).name;
if ~contains(myfile, '_thumb')
copyfile(fullfile(myfiles(i).folder, myfiles(i).name), dest);
end
end

  6 Comments

Are all the files and folders you are searching through in path1?
In path1 is only a part of the files
Try this, does the variable myfiles contain the path names of the files you want to move? Otherwise maybe I ma not undertanding problem/question.
outputFolder = fullfile(pwd, 'image_sorted');
if ~exist(outputFolder, 'dir')
mkdir(outputFolder);
end
%Folder with fubfolders that contain .tif files
input_folder = 'C:\Users\Desktop\rename_microscope_files\movie';
% Create structure with all .tif files in folder and subfolders
source =fullfile(input_folder,'**\*.tif');
myfiles = dir(source);
%Remove files with _thumb in file name. Create cell array to file to copy.
idx = ~contains({myfiles.name}, '_thumb');
myfiles = fullfile({myfiles(idx).folder}, {myfiles(idx).name});
% Loop over cell array and copy files to output folder.
for f = 1:numel(myfiles)
copyfile(myfiles{f},outputFolder);
end

Sign in to comment.


Answer by Image Analyst
on 19 Nov 2019 at 1:38

Try imageDatastore().

  0 Comments

Sign in to comment.