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

조회 수: 63 (최근 30일)
Aadil
Aadil 2012년 9월 6일
댓글: Devalla lakshmipathirao 2020년 9월 7일
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
Aadil
Aadil 2012년 9월 6일
And all your answers worked so thanks to everyone
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
  댓글 수: 3
Jan
Jan 2012년 9월 6일
I assume you need '*.pdf' with the dot.
Devalla lakshmipathirao
Devalla lakshmipathirao 2020년 9월 7일
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 .

댓글을 달려면 로그인하십시오.

카테고리

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