How to filter hidden files after calling MATLAB dir function

Using MATLAB, I need to extract an array of "correct" files from a directory. In fact, I mean that they should not be a directory, and they should not be a hidden file. Directory filtering is quite simple because the structure that returns dir has a field called isDir. However, I also need to filter out hidden files that MacOSX or Windows can put in a directory. What is the easiest cross-platform way to do this? I really don't understand how hidden files work.

+4
source share
2 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) = []; 
+5
source

Suppose all hidden files begin 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)) 
+4
source

Source: https://habr.com/ru/post/1342837/


All Articles