MATLAB Answers

How to delete empty files/spreadsheets in a directory ?

5 ビュー (過去 30 日間)
Ivan Mich
Ivan Mich 2021 年 3 月 4 日
コメント済み: Walter Roberson 2021 年 3 月 7 日
Hello,
I have a directory with milions of .xlsx files. The point is that I want to remove empty files. Is there a way to do it using a command in matlab? Instead of the fact that these files are empty, they have 10kb.
Could you please help me?
  4 件のコメント
Ivan Mich
Ivan Mich 2021 年 3 月 4 日
I should delete files that have no line of data (see the format of the EMPTY.xlsx' file I uploaded) and I should keep files that have one or more than one line of data.
Did you understand?

サインインしてコメントする。

採用された回答

Ivan Mich
Ivan Mich 2021 年 3 月 5 日
編集済み: Ivan Mich 2021 年 3 月 7 日
After your suggestions I wrote finally this script that works
clc
clear
% C = readcell('EMPTY.xlsx');
% isempty(C)
emptyfiles=dir('*xlsx')
for k=1:numel(emptyfiles)
emptynew=emptyfiles(k).name
[STATUS, SHEETS] = xlsfinfo(emptynew);
% if length(SHEETS) > 1; next; end %assume multiple sheet files are special
C = readcell(emptynew, 'sheet', SHEETS{1});
if isempty(C);
delete(emptynew);
end
end
  5 件のコメント
Walter Roberson
Walter Roberson 2021 年 3 月 7 日
if size(C,1) == 1 && size(C,2) == 1
delete(emptynew);
end

サインインしてコメントする。

その他の回答 (1 件)

Fangjun Jiang
Fangjun Jiang 2021 年 3 月 4 日
  1. run [STATUS,SHEETS] = xlsfinfo(FILENAME). Most likely, it will tell you there is only one sheet
  2. run [NUM,TXT,RAW]=xlsread(FILENAME). Most likely, isempty(NUM) and isempty(TXT) are both true
  3. delete(FILENAME)
  2 件のコメント
Ivan Mich
Ivan Mich 2021 年 3 月 5 日
I run this code after your suggestions:
[STATUS, SHEETS] = xlsfinfo('*.xlsx');
if length(SHEETS) > 1; next; end %assume multiple sheet files are special
C = readcell('*.xlsx', 'sheet', SHEETS{1});
if isempty(C); delete('*.xlsx'); end
BUT NO USE!
Do you know why?

サインインしてコメントする。

Community Treasure Hunt

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

Start Hunting!

Translated by