Delete files with specific sizes

Hello everyone,
I want to ask a question.
I have a directory of flight data files and I want to delete some files that have less size than, for example 500 kb.
Is there any way to do it in loop?
Thanks

 Respuesta aceptada

Bhaskar R
Bhaskar R el 14 de Feb. de 2020
Get the directory files
files = dir('your directory');
Apply loop and delete with your condition
for ii = 1:length(files)
if files(ii).bytes<500000 % 500kb
delete(fullfile(files(ii).folder, files(ii).name)
end
end

5 comentarios

Oktavian Jason
Oktavian Jason el 14 de Feb. de 2020
Do I have to insert the files name?
Guillaume
Guillaume el 14 de Feb. de 2020
Editada: Guillaume el 14 de Feb. de 2020
You may want to change the if to:
if files(ii).bytes<500000 & ~files(ii).isdir % 500kb
to avoid the loop erroring if there's any subfolder.
You don't even need the loop:
files = dir('your directory');
todelete = [files.bytes] < 500e3 & ~[files.isdir];
deletelist = fullfile({files(todelete).folder}, {files(todelete).name});
delete(deletelist{:});
Oktavian Jason
Oktavian Jason el 14 de Feb. de 2020
thank you very much! will try this
Oktavian Jason
Oktavian Jason el 14 de Feb. de 2020
actually i'm a bit confused how to use these. (i'm new to matlab).
can you please give me the example?
let's say my folder is "Project" in D drive, and the folder has about 600 files.
Bhaskar R
Bhaskar R el 15 de Feb. de 2020
You can learn/refer MATLAB fundamentals to apply solution to your problem. MATLAB provides very rich documentation clearly.

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre File Operations en Centro de ayuda y File Exchange.

Etiquetas

Preguntada:

el 14 de Feb. de 2020

Comentada:

el 15 de Feb. de 2020

Community Treasure Hunt

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

Start Hunting!

Translated by