How do I automatically download files from http server? urlread urlwrite

Hi,
I know urlread and urlwrite to download files form server but my problem is how I can specify parts of a string resulting from urlread which contains the name of the file I am looking for.
Please see my code: path1 = 'http://e4ftl01.cr.usgs.gov/MOLT/MOD11C1.005/2013.01.01'; str = urlread(path1); urlwrite(path2, filename);
My problem is how to extract filenames with "hdf" extension from the string?
If you run the code you will see the string is:
HTML PUBLIC "-//W3C//DTD HTML 3.2 Final//EN" html head of /MOLT/MOD11C1.005/2013.01.01</title /head body pre ******************************************************************************
U.S. GOVERNMENT COMPUTER
This US Government computer is for authorized users only. By accessing this system you are consenting to complete monitoring with no expectation of privacy. Unauthorized access or use may subject you to disciplinary action and criminal prosecution.
****************************************************************************** /pre pre<img src="/icons/blank.gif" alt="Icon "> Name Last modified Size Description<hr><img src="/icons/back.gif" alt="[DIR]"> Parent Directory -
<<-icons-image2.gif>>
BROWSE.MOD11C1.A2013001.005.2013015221704.1.jpg 15-Jan-2013 16:29 3.2M
<<-icons-image2.gif>>
BROWSE.MOD11C1.A2013001.005.2013015221704.2.jpg 15-Jan-2013 16:29 3.3M
<<-icons-unknown.gif>>
MOD11C1.A2013001.005.2013015221704.hdf 15-Jan-2013 16:29 46M
<<-icons-unknown.gif>>
MOD11C1.A2013001.005.2013015221704.hdf.xml 16-Jan-2013 02:15 32K <hr hr>&lt;/pre> </body /body>&lt;/html>
%
% I want just "MOD11C1.A2013001.005.2013015221704.hdf ".
%
% Many thanks in advance.
%
% Farz

 Accepted Answer

I had to brute-force it to see how the file address was organised. It’s easier than I thought.
This works:
url = 'http://e4ftl01.cr.usgs.gov/MOLT/MOD11C1.005/2013.01.01/MOD11C1.A2013001.005.2013015221704.hdf';
urlwrite(url, 'MOD11C1.A2013001.005.2013015221704.hdf')

4 Comments

Many Thanks, But the problem is I don't know the filename. It changes with every loop. Actually the"urlread" gives the filename to me but it is as a string and I need to extract the filename out of that. str=urlread(url). I do not know what it is I just know it has hdf extension.
I had problems pulling that string out of the file that urlread returned. I did not realise the file name changed.
I note that per isakson found a solution that was more robust than mine, so I appreciate your having Accepted my Answer.
Still this answer is helpful for me since i was using urlwrite differently and the way you wrote makes thing easier for me as it goes directly to to the file and stores it! You two helped me a lot today. I really appreciate that.

Sign in to comment.

More Answers (0)

Categories

Products

Community Treasure Hunt

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

Start Hunting!