调用MATLAB的dir函数后如何过滤隐藏文件 [英] How to filter hidden files after calling MATLAB's dir function
本文介绍了调用MATLAB的dir函数后如何过滤隐藏文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
dir
返回的结构有一个名为isDir的字段。不过,我也需要过滤掉MacOSX 或 Windows可能放入目录的隐藏文件。什么是最简单的跨平台方式来做到这一点?我不太了解隐藏文件是如何工作的。 mathworks.com/help/techdoc/ref/dir.htmlrel =nofollow> DIR 和 FILEATTRIB 检查隐藏的文件。 folder = uigetdir('请选择目录');
fileList = dir(文件夹);
#删除所有文件夹
isBadFile = cat(1,fileList.isdir); %#所有的目录都是坏的
%#循环来识别隐藏的文件
for iFile = find(〜isBadFile)'%'#只有非循环
%#on OSX ,隐藏文件以点开始
isBadFile(iFile)= strcmp(fileList(iFile).name(1),'。');
if〜isBadFile(iFile)&& ispc
%#检查隐藏的Windows文件 - 只适用于Windows
[〜,stats] = fileattrib(fullfile(folder,fileList(iFile).name));
if stats.hidden
isBadFile(iFile)= true;
end
end
end
%#删除坏文件
fileList(isBadFile)= [];
Using MATLAB, I need to extract an array of "valid" files from a directory. By valid, I mean they must not be a directory and they must not be a hidden file. Filtering out directories is easy enough because the structure that dir
returns has a field called isDir. However I also need to filter out hidden files that MacOSX or Windows might put in the directory. What is the easiest cross-platform way to do this? I don't really understand how hidden files work.
解决方案
You can combine DIR and FILEATTRIB to check for hidden files.
folder = uigetdir('please choose directory');
fileList = dir(folder);
%# remove all folders
isBadFile = cat(1,fileList.isdir); %# all directories are bad
%# loop to identify hidden files
for iFile = find(~isBadFile)' %'# loop only non-dirs
%# on OSX, hidden files start with a dot
isBadFile(iFile) = strcmp(fileList(iFile).name(1),'.');
if ~isBadFile(iFile) && ispc
%# check for hidden Windows files - only works on Windows
[~,stats] = fileattrib(fullfile(folder,fileList(iFile).name));
if stats.hidden
isBadFile(iFile) = true;
end
end
end
%# remove bad files
fileList(isBadFile) = [];
这篇关于调用MATLAB的dir函数后如何过滤隐藏文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文