필터 지우기
필터 지우기

How to select files in a directory

조회 수: 20 (최근 30일)
K BV
K BV 2013년 1월 22일
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 !

채택된 답변

Walter Roberson
Walter Roberson 2013년 1월 22일
편집: Walter Roberson 2013년 1월 22일
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.

추가 답변 (3개)

Azzi Abdelmalek
Azzi Abdelmalek 2013년 1월 22일
편집: Azzi Abdelmalek 2013년 1월 22일
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 2013년 1월 22일
% 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 2013년 1월 22일
Thank you all for your answers ! That helped me extracting the files I need !

카테고리

Help CenterFile Exchange에서 File Operations에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by