MATLAB Answers

Aadil
1

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

Asked by Aadil
on 6 Sep 2012
Latest activity Commented on by Dev-iL on 22 May 2016
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

  0 Comments

Sign in to comment.

2 Answers

Answer by Azzi Abdelmalek
on 6 Sep 2012
Edited by Azzi Abdelmalek
on 6 Sep 2012
 Accepted Answer

any(size(dir([yourFolderName '/*.pdf' ]),1))

  9 Comments

A quick restart of matlab seems to have sorted it out!
Looks like I was overworking matlab : D
And all your answers worked so thanks to everyone
It's probably better (in terms of different OS support) to use fullfile instead of constructing the path manually.

Sign in to comment.


Answer by Dr. Seis
on 6 Sep 2012
Edited by Dr. Seis
on 6 Sep 2012

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

  2 Comments

Ok i'll give this a go, Thanks
I assume you need '*.pdf' with the dot.

Sign in to comment.