How to select files in a directory

31 vues (au cours des 30 derniers jours)
K BV
K BV le 22 Jan 2013
Hello,
I created a directory which contains a lot of DICOM files (IM_0001, IM_0004, ..., IM_0025, IM_0028, IM_0031, ..., IM_0052, ..., IM_0088) using :
listing = dir('IM*.*');
I would like to select only files which names are between IM_0025 and IM_0052 (IM_0025, IM_0028, IM_0031, ..., IM_0052) and save them in another directory.
Is there any function that may help me ?
Thank you in advance !

Réponse acceptée

Walter Roberson
Walter Roberson le 22 Jan 2013
Modifié(e) : Walter Roberson le 22 Jan 2013
listing = dir('IM*.*');
for K = 1 : length(listing)
fname = listing(K).name;
if ~strcmp(fname(1:3), 'IM_'); continue; end
fnum = str2double(fname(4:7));
if isnan(fnum) || fnum < 25 || fnum > 52; continue; end
copyfile(fname, 'NewDirectoryNameGoesHere');
end
or use movefile() instead of copyfile() if you want them moved instead of duplicated.

Plus de réponses (3)

Azzi Abdelmalek
Azzi Abdelmalek le 22 Jan 2013
Modifié(e) : Azzi Abdelmalek le 22 Jan 2013
d=struct2cell(dir('IM*.*'));
name=d(1,:)
for k=1:numel(name)
file=name{k}
v=str2num(file(6:end))
If v>=25 & v<=52
copyfile(file,'yourfolder')
end

Thorsten
Thorsten le 22 Jan 2013
% run once for, see if the 'move file' output is ok
% if ok, uncomment the movefile line such that the files are actually moved
listing = dir('IM*.*');
dstdir = './newdir'; % where the selected files should be moved
for i = 1:numel(listing)
filename = d(i).name;
[num elements_matched] = sscanf(filename, 'IM_%d');
if elements_matched && num >= 25 && num <= 52
disp(['move file ' filename ' to ' dstdir '.'])
% movefile(filename, dstdir)
end
end

K BV
K BV le 22 Jan 2013
Thank you all for your answers ! That helped me extracting the files I need !

Catégories

En savoir plus sur File Operations dans Help Center et File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by