Filter löschen
Filter löschen

How to select files in a directory

20 Ansichten (letzte 30 Tage)
K BV
K BV am 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 !

Akzeptierte Antwort

Walter Roberson
Walter Roberson am 22 Jan. 2013
Bearbeitet: Walter Roberson am 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.

Weitere Antworten (3)

Azzi Abdelmalek
Azzi Abdelmalek am 22 Jan. 2013
Bearbeitet: Azzi Abdelmalek am 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 am 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 am 22 Jan. 2013
Thank you all for your answers ! That helped me extracting the files I need !

Kategorien

Mehr zu File Operations finden Sie in Help Center und File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by