How to delete all Files not listed in "FileName" variable?

조회 수: 7 (최근 30일)
Smithy
Smithy 2022년 12월 7일
편집: Stephen23 2023년 1월 3일
Hello everybody,
I have the file lists to be kept and other files should be deleted from the folders.
I tried as below. and error occures that Error using delete, Name must be a text scalar.
Is there a way to delete the files not listed in "FileName" variable?
clear; close all; clc;
% viriable "FileName" is the image file to be kept.
% and other files not listed in "FileName" should be deleted.
FileName = ["q1.jpg";"q2.jpg";"q3.jpg"];
temp = table(FileName);
MyFolderInfo = dir(fullfile(pwd, '*'));
MyFolderInfo = MyFolderInfo(~ismember({MyFolderInfo.name},{'.','..'}));
fileList = string({MyFolderInfo.name});
noNeed = [];
for i=1:length(fileList)
if ~strcmp(fileList(i),FileName)
noNeed = [noNeed; fileList(i)];
end
end
F = fullfile(pwd,noNeed);
delete(F) % Errr occures of Error using delete, Name must be a text scalar.

채택된 답변

Walter Roberson
Walter Roberson 2022년 12월 7일
Use
arrayfun(@delete, F)
  댓글 수: 2
Smithy
Smithy 2022년 12월 7일
편집: Stephen23 2023년 1월 3일
It works perfectly. Thank you very much. I really appreciate with it.
Voss
Voss 2022년 12월 7일
Another option:
delete(F{:})

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Time Series Events에 대해 자세히 알아보기

태그

제품


릴리스

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by