List all and only files with no extension

72 ビュー (過去 30 日間)
Sam Smith
Sam Smith 2018 年 11 月 20 日
編集済み: franco otaola 2020 年 8 月 18 日
How do I list all files in a folder without any extension?
  3 件のコメント
Sam Smith
Sam Smith 2018 年 11 月 20 日
I've attached a copy of the file, which I renamed to have .txt at the end, as the system wouldn't let me upload files with no extension.
They come from Betfair, and they have no extension.
Walter Roberson
Walter Roberson 2018 年 11 月 20 日
no extension is perfectly valid and common on Unix systems

サインインしてコメントする。

採用された回答

Jan
Jan 2018 年 11 月 20 日
編集済み: Jan 2018 年 11 月 22 日
Folder = 'C:\Temp'; % Your folder
FileList = dir(fullfile(Folder, '*'));
NameList = {FileList.name};
% NameList(ismember(NameList, {'.', '..'})) = []; % Remove . and .. Not required here
hasDot = contains(NameList, '.');
NameList(hasDot) = []; % Removes . and .. also
Now this is the list of all files, which do not contain a dot and there do not have a file extension. If you want to exclude folders also:
NameList(hasDot || [Filelist.isdir]) = [];
If you want to search recursively in subfolders also:
FileList = dir(fullfile(Folder, '**', '*'));
isFile = [FileList.isdir];
NameList = {FileList(isFile).name};
PathList = {FileList(isFile).folder};
noDot = ~contains(NameList, '.');
File = fullfile(PathList(noDot), NameList(noDot));
  4 件のコメント
Jan
Jan 2018 年 11 月 22 日
Answer edited: Subfolders excluded on demand and recursive search.
@Walter: A ytpo: the field is called 'isdir', not 'isfolder'.
franco otaola
franco otaola 2020 年 8 月 18 日
編集済み: franco otaola 2020 年 8 月 18 日
hello @jan I get an error when i add the subfolder removing. i am geting :
Operands to the || and && operators must be convertible to logical scalar values.
i do not get why as the two are logical (i created a variable isFolder=[filesStruc.isdir]; the two hasDot and isFolder are logical of the same length (in my case 1x5) but still i get that error (v2017b).
EDIT: hello i have found the problem, as mentioned here (link) it should use | and not || so there is a small error in
NameList(hasDot || [Filelist.isdir]) = [];
it should be
NameList(hasDot | [Filelist.isdir]) = [];

サインインしてコメントする。

その他の回答 (2 件)

Sam Smith
Sam Smith 2018 年 11 月 21 日
I found that
dir(foldername,'*.')
works too.
  1 件のコメント
Walter Roberson
Walter Roberson 2018 年 11 月 22 日
Too many input arguments -- dir only accepts a single input.
If you repair that to
dir(fullfile(foldername, '*.'))
then it will not do the desired task on Mac or Linux systems: on those systems it will only look for names that end in a literal '.', such as the '.' and '..' entries. On Mac and Linux, lack of a file extension is not treated as the pair (basic_name, empty_extension) the way it might be in DOS 3.1 : On Mac and Linux, '.' is just another character other than '.' as the complete component refers to the current directory, '..' as the complete component refers to the parent directory, and convention that files with '.' as a leading character will not be listed by default in directory listings.

サインインしてコメントする。


Walter Roberson
Walter Roberson 2018 年 11 月 20 日
dir() to get the directory content . Extract the file names to a cellstr. cellfun @fileparts with three outputs and uniform 0. cellfun @isempty the third output . The nonzero locations correspond to files with no extension whose full information you can get from the dir you ran

カテゴリ

Help Center および File ExchangeFile Operations についてさらに検索

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by