Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 747
JnBrymn Avatar asked Dec 28 '22 00:12

JnBrymn


1 Answers

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) = [];
like image 191
Jonas Avatar answered Jan 25 '23 22:01

Jonas