How can I automatically download from a url?
    5 views (last 30 days)
  
       Show older comments
    
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
0 Comments
Answers (2)
  Image Analyst
      
      
 on 16 Jul 2013
        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?
0 Comments
  Jim Hokanson
      
 on 16 Jul 2013
        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 Comments
  Javier
 on 16 Jul 2013
				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
See Also
Categories
				Find more on Downloads in Help Center and File Exchange
			
	Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!