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

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 日

4 投票

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

9 件のコメント

Aadil
Aadil 2012 年 9 月 6 日
Thanks but I forgot to mention I need a true or false output
José-Luis
José-Luis 2012 年 9 月 6 日
編集済み: José-Luis 2012 年 9 月 6 日
~isempty( dir ( fullfile ( your_path , '*.pdf' ) ) );
True if there is at least one pdf.
False if there is no pdf.
Aadil
Aadil 2012 年 9 月 6 日
How does dir work?
It seems to find create an output array not matter what file extension I put:
>> dir('*.bollocks')
ans =
10x1 struct array with fields:
name
date
bytes
isdir
datenum
Jan
Jan 2012 年 9 月 6 日
See "help dir". It looks for the files in the current folder, when no folder has been specified. See the "fullfile" in Jose-Luis example.
It seems, like you have 10 files matching the filters "*.bollocks" in the current folder.
José-Luis
José-Luis 2012 年 9 月 6 日
That means you do have 10 files with the extension .bollocks in your working directory. I am not one to judge file names :P, but just to make sure, i tested in my machine and here is what i got.
bla = dir('*.youdontexist')
bla =
0x1 struct array with fields:
name
date
bytes
isdir
datenum
I would suggest you create an empty directory, make it your working directory and test again. If you have the same results, then we have an interesting problem.
Azzi Abdelmalek
Azzi Abdelmalek 2012 年 9 月 6 日
any(size(dir([yourFolderName '/*.pdf' ]),1))
Aadil
Aadil 2012 年 9 月 6 日
A quick restart of matlab seems to have sorted it out!
Looks like I was overworking matlab : D
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 日

0 投票

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 件のコメント

Aadil
Aadil 2012 年 9 月 6 日
Ok i'll give this a go, Thanks
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 .

サインインしてコメントする。

カテゴリ

ヘルプ センター および File ExchangeFile Operations についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by