How to delete empty files/spreadsheets in a directory ?

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

data = {};
writecell(data, 'test.xlsx');
!ls -l test.xlsx
-rw-r--r-- 1 mluser worker 2053 Mar 4 20:15 test.xlsx
If I understand correctly, you are indicating that the files have no visible content, but still take up about 10 kb of space.
If so then that would suggest that they might have template or macro material written into them, or might have additional sheets.
How do you want to decide whether a particular spreedsheet is empty enough for your purposes?
If I open some files the have one line of data, whereas some somes have no data.
The .xlsx files are outcome of a loop which creates multiple files.
I am importing you two examples of files (one empty and one with data)
To clarify:
Files that have only one line of data should be deleted, but files that have more than one line of data should not be deleted?
Ivan Mich
Ivan Mich 2021년 3월 4일
편집: 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

This script won't work, if you've run it!
I modify it. Now it works. I have one question. If I want to specify it in order to delete files that have empty only the second column how could I make it? I mean one file that has only the first column with data and all the others are empty how could this file be deleted , after a code modification?
Thank you in advance
if isempty(C) || size(C,2) < 2
delete(emptynew);
end
Ok, one last question. Can I delete files that have only 1 row and one column empty (I mean 1x1 double) and keep all the other files?
if size(C,1) == 1 && size(C,2) == 1
delete(emptynew);
end

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

추가 답변 (1개)

Fangjun Jiang
Fangjun Jiang 2021년 3월 4일

0 개 추천

  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

Alternative to the second step:
C = readcell(FILENAME);
isempty(C)
For example,
[STATUS, SHEETS] = xlsfinfo(FILENAME);
if length(SHEETS) > 1; next; end %assume multiple sheet files are special
C = readcell(FILENAME, 'sheet', SHEETS{1});
if isempty(C); delete(FILENAME); end
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?

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

카테고리

도움말 센터File Exchange에서 Variables에 대해 자세히 알아보기

질문:

2021년 3월 4일

댓글:

2021년 3월 7일

Community Treasure Hunt

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

Start Hunting!

Translated by