How to filter hidden files after calling MATLAB's dir function

隐身守侯 提交于 2019-12-01 07:35:59
Jonas

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) = [];
Amit Joshi

Assuming all hidden files start with '.'. Here is a shortcut to remove them:

s = dir(target); % 'target' is the investigated directory

%remove hidden files
s = s(arrayfun(@(x) ~strcmp(x.name(1),'.'),s))
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!