check if a file exists

조회 수: 4,236(최근 30일)
Salvatore Mazzarino
Salvatore Mazzarino 2012년 9월 29일
댓글: sesilia maidelin 2021년 7월 22일
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?

채택된 답변

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.
  댓글 수: 5
sesilia maidelin
sesilia maidelin 2021년 7월 22일
hi there, thanks for replying @Jan , in my case i just want to know the existence of a particular file type in a folder, then move the folder to another parentfolder named complete. so if the folder AAA contains a .txt and .jpgs, i want to move AAA into the folder complete that contains folder AAZ, BBA, etc that also contain .txt and .jpg files. i attached the picture of how the source file look like. I want to move files like 00000715 to the folder complete. Thank you!

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

추가 답변(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
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
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

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
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
  댓글 수: 2
Image Analyst
Image Analyst 2020년 10월 11일
No. If you don't know the filename(s), you cannot determine if a file exists. The best you could do is to see if ANY files exist in the folder by using the dir() function.

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

태그

Community Treasure Hunt

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

Start Hunting!

Translated by