Removing Hidden Files on Windows

1 回表示 (過去 30 日間)
Amul Choudhary
Amul Choudhary 2016 年 8 月 11 日
回答済み: Fangjun Jiang 2016 年 8 月 11 日
I need to create a list of all the excel files in a directory so that I can cycle through them. Here's what I've been using.
path='C:\Users\choudhara2\Desktop\Janelle''s File\';
sourceFiles=dir(fullfile(path,'*.xlsx'));
This works. I then need to remove any hidden files that may show up in sourceFiles. This is what I've written:
for k=length(sourceFiles):-1:1
fname=sourceFiles(k).name
if fname(1)=='.'
sourceFiles(k)=[];
end
end
sourceFiles(~cellfun('is empty',sourceFiles))
However I get the error: cellfun is only valid for types cell. Any suggestions to get around this without screwing up any of the code after that?
  3 件のコメント
Amul Choudhary
Amul Choudhary 2016 年 8 月 11 日
Sorry thats just a type, Ill edit the question.
Adam
Adam 2016 年 8 月 11 日
Doing this:
sourceFiles(k)=[];
deletes elements from your struct array so you will only be left with the remaining files anyway so you shouldn't need to do any isempty testing. Only a cell array can contain empty cells - if you set an element of a numeric or struct array (such as the output of dir) to [] it gets deleted instead.
I assume
'is empty'
is a typo too that should be 'isempty', but as I say shouldn't be needed anyway.

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

回答 (2 件)

Image Analyst
Image Analyst 2016 年 8 月 11 日
編集済み: Image Analyst 2016 年 8 月 11 日
You should NOT overwrite path. It's an important built-in variable and overwriting it will cause tons of problems. Call it folder instead of path.
Next, if you specify a *.xlsx file pattern, you will NOT get hidden files like . and .. (dot and dot dot).
Even if you did, you would use isdir() to check for . and .. rather than ==.
Finally, don't delete rows from an array in a loop because the array shifts up after that. For example if you delete #4, then what was #4 is gone and the new #4 is what used to be #5. So on the next iteration you'll skip that #5 file entirely, and start again with what used to be #6. You should build a vector called "rowsToDelete" and then after the loop is done call sourceFiles(rowsToDelete) = []. This will avoid skipping any files. But again you shouldn't even need to use a loop because you won't have hidden files in the first place.

Fangjun Jiang
Fangjun Jiang 2016 年 8 月 11 日
"path" is a MATLAB command/keyword. Try not to use it as variable name.
dir('*.xlsx') won't return "." and ".." so you don't need that processing.
sourceFiles is a struct array, not cell array. That is why the error was given.

カテゴリ

Help Center および File ExchangeFile Operations についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by