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

61 visualizzazioni (ultimi 30 giorni)
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

Risposta accettata

Azzi Abdelmalek
Azzi Abdelmalek il 6 Set 2012
Modificato: Azzi Abdelmalek il 6 Set 2012
any(size(dir([yourFolderName '/*.pdf' ]),1))
  9 Commenti
Aadil
Aadil il 6 Set 2012
And all your answers worked so thanks to everyone
Dev-iL
Dev-iL il 22 Mag 2016
It's probably better (in terms of different OS support) to use fullfile instead of constructing the path manually.

Accedi per commentare.

Più risposte (1)

Dr. Seis
Dr. Seis il 6 Set 2012
Modificato: Dr. Seis il 6 Set 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
  3 Commenti
Jan
Jan il 6 Set 2012
I assume you need '*.pdf' with the dot.
Devalla lakshmipathirao
Devalla lakshmipathirao il 7 Set 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 .

Accedi per commentare.

Categorie

Scopri di più su File Operations in Help Center e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by