How can I automatically download from a url?

4 次查看(过去 30 天)
The following code is for automatically download the most recent hdf file from MODIS satellite ftp. They have recently changed from ftp to http. I can not modify the code.
%Download the last hdf from MIDIS for specified PATH & Tile----------------------
function FILE = MODIS_downloader(PATH,TILE,DATE)
FTP = ftp('e4ftl01.cr.usgs.gov');
YEAR = year(DATE);
MONTH = month(DATE);
for y = YEAR:-1:2000
for m = MONTH:-1:1
if (y==YEAR && m == MONTH)
LAST_DAY = day(DATE);
else
LAST_DAY = eomday(y,m);
end
for d = LAST_DAY:-1:1
FOLDER = [PATH, num2str(y), '.', pad(num2str(m), 2), '.', pad(num2str(d), 2)];
FLIST = dir(FTP, FOLDER) ;%in this ftp(FTP), go to this folder & grab the list of files
if ~isempty(FLIST)
cd(FTP, FOLDER);
for i = 1:length(FLIST) %No of files & folders within the current folder of ftp
FNAME = FLIST(i).name;% returns the name of the file in that dir
if ~(isempty(strfind(FNAME, TILE)) || isempty(strfind(FNAME, 'hdf')))
if strfind(FNAME, 'hdf') == (length(FNAME) - 2) %ensures that hdf is a file's extension and not just a part of the file name
FILE = mget(FTP, FNAME);
close(FTP);
return
end
end
end
end
end
end
end
end

回答(2 个)

Image Analyst
Image Analyst 2013-7-16
If you can't modify that code then I think we're done. You have to modify it to change the behavior. If you could modify it, I'd maybe suggest looking at urlread() instead of ftp() and see if that gets you anywhere. Why can't you modify it? Will someone complain, or will it break something else if you modify it?

Jim Hokanson
Jim Hokanson 2013-7-16
Use urlwrite to retrieve the file. Unfortunately http does not support directory enumeration like FTP does so you'll need to know where the file is located.
  2 个评论
Javier
Javier 2013-7-16
Hello Jim, I usually use URLWRITE for download a specific file via ftp, but if I wanna download a entire folder via FTP, I can do it?
REGARDS

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Downloads 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by