Search code examples
matlabfile-extensiondir

List files that don't have any extension using command `dir`


In a directory containing files with different extensions, e.g. .ext1, .ext2, and (no extension), how can I use the dircommand to list only the files that don't have any extension?

The command dir(fullfile('path/to/dir','*.ext1')) will list all .ext1 files, but I don't know of any option to read extension-less files.


Solution

  • Try if the following fits all your needs:

    allfiles = dir
    filelist = {allfiles(3:end).name}
    
    mask = cellfun(@isempty, regexp( filelist ,'[^\\]*(?=[.][a-zA-Z]+$)','match'))
    output = filelist(mask)
    

    The regular expression finds all filenames which have an extension and returns an empty array if not. Therefore cellfun(@isempty, ... ) will give you the desired mask.