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

前端 未结 2 1150
情歌与酒
情歌与酒 2021-01-13 23:13

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 o

2条回答
  •  生来不讨喜
    2021-01-13 23:39

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

提交回复
热议问题