How to display an error if a file doesn't exist
    12 次查看(过去 30 天)
  
       显示 更早的评论
    
I am working on a function that uses ftp to pull files from nasa's website. I want to make it so that if the file doesn't exist then display an error message. The following is the code of what is retrieved.
ftpobj = ftp('cddis.nasa.gov');
for i=1:days2get 
    dirStr = sprintf('/gnss/data/daily/2020/brdc/', doy(i)); 
    fileStr = sprintf('BRDC00IGS_R_2020%03d0000_01D_MN.rnx.gz', doy(i));  
    cd(ftpobj, dirStr);     
    mget(ftpobj, fileStr, '.');  
end
gunzip('*.gz'); 
Right now if the file doesn't exist I get an error saying that the file is not found on the server.
0 个评论
采纳的回答
  dpb
      
      
 2020-7-1
        ftpobj = ftp('cddis.nasa.gov');
dirStr = sprintf('/gnss/data/daily/2020/brdc/'; 
cd(ftpobj, dirStr);     
for i=1:days2get 
  fileStr = sprintf('BRDC00IGS_R_2020%03d0000_01D_MN.rnx.gz', doy(i));
  d=dir(ftpobj,fileStr);
  if isempty(d)
    f = errordlg(['File: ' fileStr ' not found','File Error');
    continue
  end
  mget(ftpobj, fileStr, '.');  
end
gunzip('*.gz'); 
The dirStr has a what appears superfluous doy(i) after it -- didn't look like the site had additional structure; the files as listed were at the above directory.
Above will popup a dialog; you can do whatever instead -- the trick is to see if the dir() call is/is not empty.  If the file exists, it'll be an ordinary-looking MATLAB dir() output struct.
Above worked when tried here...
1 个评论
  dpb
      
      
 2020-7-1
				Everybody else was busy, too, I see!  :)
I was going to modify above as the easiest solution is try...catch block.
But I see IA used it above.
更多回答(2 个)
  Image Analyst
      
      
 2020-7-1
        Try this:
doy = [30, 200];
days2get = length(doy);
ftpObject = ftp('cddis.nasa.gov');
for k = 1 : days2get
	folder = sprintf('/gnss/data/daily/2020/brdc/', doy(k));
	baseFileName = sprintf('BRDC00IGS_R_2020%03d0000_01D_MN.rnx.gz', doy(k));
	cd(ftpObject, folder);
	try
		mget(ftpObject, baseFileName, '.');
		fprintf('\nSUCCESS!  FTP Successfully retrieved filename #%d of %d:\n   %s\nfrom folder:\n   %s\n', ...
			k, days2get, baseFileName, folder);
	catch
		errorMessage = sprintf('ERROR!  FTP Error retrieving filename #%d of %d:\n   %s\nfrom folder:\n   %s', ...
			k, days2get, baseFileName, folder);
		fprintf('\n%s\n', errorMessage);
		uiwait(errordlg(errorMessage));
	end
end
gunzip('*.gz');
You'll see:
SUCCESS!  FTP Successfully retrieved filename #1 of 2:
   BRDC00IGS_R_20200300000_01D_MN.rnx.gz
from folder:
   /gnss/data/daily/2020/brdc/
ERROR!  FTP Error retrieving filename #2 of 2:
   BRDC00IGS_R_20202000000_01D_MN.rnx.gz
from folder:
   /gnss/data/daily/2020/brdc/
and the user will get an error popup dialog box whenever there is an error retrieving a file.
0 个评论
  Hussein Ammar
      
 2020-7-1
        if ~exist('myFileName.EXTENSION','file')
    disp('Requested file was not found.')
end
0 个评论
另请参阅
类别
				在 Help Center 和 File Exchange 中查找有关 File Operations 的更多信息
			
	Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!



