Delete files with specific names
이전 댓글 표시
Hello
I have a directory with files from the same type with names like:
2016_03_24_09 -0002-0001-00058.dcm
2016_03_24_09 -0002-0001-00059.dcm
2016_03_24_09 -0002-0001-00060.dcm
etc.
Based on the last 5 figures (e.g.,*00060.dcm),
I need to delete every file that ends with a number greater than 276.
Is there a way to do it in a loop?
Thanks,
Tali
댓글 수: 4
John
2016년 5월 31일
files = ls('*.dcm'); % list of files with .dcm extension
for n = 1:N
ind1 = strfind(files(n,:), '-'); % indicies of the '-' char
ind2 = strfind(files(n,:), '.'); % index of '.'
str = files(n,ind1(end)+1:ind2-1);% extract the numeric string
if str2num(str) > 276
delete(files(n,:)); % delete if greater than 276
end
end
Tali Leibovich
2016년 5월 31일
Image Analyst
2016년 5월 31일
You need
for n = 1: size(files, 1)
and, since using ls gives you a character array instead of a cell array, you'll need
delete(strtrim(files(n,:)));
to trim off the extra whitespace. If you use dir() instead of ls(), you don't need to use strtrim().
Tali Leibovich
2016년 5월 31일
채택된 답변
추가 답변 (0개)
카테고리
도움말 센터 및 File Exchange에서 File Operations에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!