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

108 views (last 30 days)
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
EDIT: I will need the output as true or false

Accepted Answer

Azzi Abdelmalek
Azzi Abdelmalek on 6 Sep 2012
Edited: Azzi Abdelmalek on 6 Sep 2012
any(size(dir([yourFolderName '/*.pdf' ]),1))
Dev-iL on 22 May 2016
It's probably better (in terms of different OS support) to use fullfile instead of constructing the path manually.

Sign in to comment.

More Answers (1)

Dr. Seis
Dr. Seis on 6 Sep 2012
Edited: 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"
% there is a "pdf" file in your directory
% dir_struct(1).name gives the file name of the first "pdf" file listed
Devalla lakshmipathirao
Devalla lakshmipathirao on 7 Sep 2020
Using the code by @Dr. Seis, how can i check if .pdf exists i.e, not based on number of elements but rather by checking the file name .

Sign in to comment.


Find more on File Operations in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by