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

7 Ansichten (letzte 30 Tage)
Smithy
Smithy am 7 Dez. 2022
Bearbeitet: Stephen23 am 3 Jan. 2023
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.

Akzeptierte Antwort

Walter Roberson
Walter Roberson am 7 Dez. 2022
Use
arrayfun(@delete, F)
  2 Kommentare
Smithy
Smithy am 7 Dez. 2022
Bearbeitet: Stephen23 am 3 Jan. 2023
It works perfectly. Thank you very much. I really appreciate with it.

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Mehr zu Time Series Events finden Sie in Help Center und File Exchange

Tags

Produkte


Version

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by