MATLAB Answers

check if a file exists

4,103 ビュー (過去 30 日間)
Salvatore Mazzarino
Salvatore Mazzarino 2012 年 9 月 29 日
編集済み: Jan 2019 年 5 月 6 日
I'm using the "importdata" function to import data from a text file. I would like to have a way to check if the text file exists before attempting to import data from the file. If the file doesn't exist, I would then like to display a custom error message. What is the function that I should use?

  0 件のコメント

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

採用された回答

Jan
Jan 2012 年 9 月 29 日
編集済み: MathWorks Support Team 2018 年 11 月 9 日
Starting in R2017b, you can use the "isfile" function to check if a file exists. For example:
if isfile(filename)
% File exists.
else
% File does not exist.
end
The "isfile" function searches for files only on the specified path or in the current folder.
For R2017a and previous releases, use the "exist" function. For example:
if exist(filename, 'file') == 2
% File exists.
else
% File does not exist.
end
Be sure to specify an absolute path for the file name. The "exist" function searches all files and folders on the search path, which can lead to unexpected results if multiple files with the same name exist.

  2 件のコメント

shuchita bahadur
shuchita bahadur 2017 年 1 月 6 日
thanks, works
Muhammad Asad
Muhammad Asad 2019 年 1 月 29 日
It is mentioned that
"Be sure to specify an absolute path for the file name. The "exist" function searches all files and folders on the search path, which can lead to unexpected results if multiple files with the same name exist."
Can you please elaborate with example

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

その他の回答 (5 件)

Image Analyst
Image Analyst 2012 年 9 月 29 日
if exist(fullFileName, 'file')
% File exists. Do stuff....
else
% File does not exist.
warningMessage = sprintf('Warning: file does not exist:\n%s', fullFileName);
uiwait(msgbox(warningMessage));
end

  3 件のコメント

Valentino
Valentino 2015 年 4 月 27 日
Works perfect, thanks!
Nelson dos Santos Netto
Nelson dos Santos Netto 2016 年 3 月 4 日
Thanks for the information! Btw, I was wondering if this check is purely done regarding the file name or the content in the file itself...The thing is:I'm trying to develop a script to regularly copy files from a sourcefolder to a destinationfolder, but I wanted to make sure I copy only the most recent and modified data, in order to get a more efficient and quicker programme. Could you help me out, please?? Tks a lot!
Image Analyst
Image Analyst 2016 年 3 月 4 日
It checks if a file with that filename is there. It does not check what the contents are, or even if there are contents. It's possible to have a file with zero bytes. You can use fileInfo = dir(filename) to check size.

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


Azzi Abdelmalek
Azzi Abdelmalek 2012 年 9 月 29 日
編集済み: Azzi Abdelmalek 2012 年 9 月 29 日
a=dir % or a=dir('folder')
b=struct2cell(a)
any(ismember(b(1,:),'filename.txt'))

  2 件のコメント

goc3
goc3 2019 年 5 月 6 日
I prefer this answer, as it only checks in the current directory.
Jan
Jan 2019 年 5 月 6 日
But if the current directory contains a huge number of files, it is a waste of time. If the current folder is in a network drive, the slow down is substantial. This is more efficient:
isfile(fullfile(cd, 'filename.txt'))
And if it is unknown, if the file contains a path or not, use https://www.mathworks.com/matlabcentral/fileexchange/28249-getfullpath

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


José-Luis
José-Luis 2012 年 9 月 29 日
編集済み: José-Luis 2012 年 9 月 29 日
doc exist

  0 件のコメント

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


Simon
Simon 2016 年 11 月 2 日
編集済み: Simon 2016 年 11 月 2 日
A more modern / more succinct approach:
assert( exist( filename, 'file ) == 2, 'File not found.' );
Assert is a wonderful thing, and makes error-catching code a lot more compact than lots of if blocks! The way it works is,
assert( condition, error );
where condition is something that returns a logical true or false, and error is the message that results if it's false.

  2 件のコメント

Miguel Diaz
Miguel Diaz 2016 年 11 月 17 日
This is a very nice solution. Except exist(filename,'file') == 2 would occur when the file DOES exist. so a corrected version of this would be:
assert(exist(filename,'file') ~= 2, 'File not found.' );
Overall, nice solution my friend!
Image Analyst
Image Analyst 2016 年 11 月 17 日
Though a friendlier more informative error message (like in my answer) would be an improvement.
But you're not totally correct. Like you said "exist(filename,'file') == 2 would occur when the file DOES exist." and exist(filename,'file') == 2 would be FALSE when the file DOES NOT exist. Since assert only spits out the error message when the file does not exist (the condition is false), it was correct as Simon originally had it, not your "correction".
Remember, in summary, if the file does exist, the condition is true, and the 'File not found.' message is not printed to the command window. If the file does NOT exist, the condition is false, and so the 'File not found.' message is printed to the command window.

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


ABHAY K K
ABHAY K K 2019 年 2 月 4 日
is there a way to check the files exists in a particular directory without using the filename, What i mean to say is the more generalised way to find out

  0 件のコメント

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

カテゴリ

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by