필터 지우기
필터 지우기

Delete files with a specific string

조회 수: 32 (최근 30일)
Ahmed Abdulla
Ahmed Abdulla 2022년 1월 17일
댓글: Ahmed Abdulla 2022년 1월 17일
In my directory I have 1000's of files, however some files are not needed and they all have the string "Yes_" in their file names, is there an easy way to delete all files that contain this phrase in their name?
  댓글 수: 2
Stephen23
Stephen23 2022년 1월 17일
"is there an easy way to delete all files that contain this phrase in their name?"
delete *Yes_*.*
Ahmed Abdulla
Ahmed Abdulla 2022년 1월 17일
Wow this works and really simple. Thank you

댓글을 달려면 로그인하십시오.

채택된 답변

Image Analyst
Image Analyst 2022년 1월 17일
Try this:
% Specify the folder where the files live.
myFolder = pwd; % or wherever . . . 'C:\Users\yourUserName\Documents\My Pictures';
% Check to make sure that folder actually exists. Warn user if it doesn't.
if ~isfolder(myFolder)
errorMessage = sprintf('Error: The following folder does not exist:\n%s\nPlease specify a new folder.', myFolder);
uiwait(warndlg(errorMessage));
myFolder = uigetdir(); % Ask for a new one.
if myFolder == 0
% User clicked Cancel
return;
end
end
% Get a list of all files in the folder with the desired file name pattern.
filePattern = fullfile(myFolder, '*.*'); % Change to whatever pattern you need.
theFiles = dir(filePattern);
for k = 1 : length(theFiles)
baseFileName = theFiles(k).name;
fullFileName = fullfile(theFiles(k).folder, baseFileName);
fprintf(1, 'Now checking %s\n', fullFileName);
% Now do whatever you want with this file name,
% such as deleting it.
if contains(baseFileName, 'Yes_', 'IgnoreCase', true)
fprintf(1, ' Now deleting %s\n', fullFileName);
recycle on % Go to recycle bin
delete(fullFileName);
end
end
Adapted from the FAQ:

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Startup and Shutdown에 대해 자세히 알아보기

제품


릴리스

R2019a

Community Treasure Hunt

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

Start Hunting!

Translated by