How to check if a file of certain type exists in folder?

25 次查看(过去 30 天)
Hi All,
How can I check if a .pdf file exists in a folder
I know I can use the Exist command if I know the full filename, but I don't have the file name
Thanks,
EDIT: I will need the output as true or false

采纳的回答

Azzi Abdelmalek
Azzi Abdelmalek 2012-9-6
编辑:Azzi Abdelmalek 2012-9-6
any(size(dir([yourFolderName '/*.pdf' ]),1))
  9 个评论
Dev-iL
Dev-iL 2016-5-22
It's probably better (in terms of different OS support) to use fullfile instead of constructing the path manually.

请先登录,再进行评论。

更多回答(1 个)

Dr. Seis
Dr. Seis 2012-9-6
编辑:Dr. Seis 2012-9-6
You could try doing something like:
dir_struct = dir( fullfile(your_path,'*pdf') );
if numel(dir_struct) == 0
% then there are no files with extension "pdf"
else
% there is a "pdf" file in your directory
% dir_struct(1).name gives the file name of the first "pdf" file listed
end

类别

Help CenterFile Exchange 中查找有关 File Operations 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by