Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

How would I get a list of filenames available at a URL? For example, list the files at:

share|improve this question
i know javascript : document.getElementsByTagName('a').length - 4 removed 4 because of parent directory ,and 4 label links – vishal sharma Oct 2 '13 at 18:04
up vote 19 down vote accepted

Does this do the job?

links = Import[


then you can cherry-pick the ones you want using, eg,StringCases:

DeleteCases[StringCases[links, __ ~~ "gif"], {}]

 {""}, ...
share|improve this answer
Much easier than mine :) I am sure that I looked through the docs of Import and couldn't find a reference to "Hyperlinks". If I had, I wouldn't have gone down the R route. – WalkingRandomly Oct 2 '13 at 10:04
It does return some links that aren't files though such as "…; – WalkingRandomly Oct 2 '13 at 10:14
@WalkingRandomly True, but that's easily fixed. – cormullion Oct 2 '13 at 10:51

As is commonly recommended in other Q&As for tasks such as this, we can also use Cases:

baseurl = "";
source = Import[baseurl, "XMLObject"];
baseurl <> # & /@ Rest@Cases[source, XMLElement["td", __,
     {XMLElement["a", __, {___ ..., c_, ___ ...}]}
     ] :> c, Infinity]

{"", "", "", "",...

Or just remove baseurl <> # & /@ if you don't want the full URLs, but only the file names.

share|improve this answer

Although I am sure that this can be done in Mathematica, I already know how to do it in R. For example, see the answer at

Thanks to RLink, I can use this solution directly in Mathematica

(*Initialise R*)
(*Install the XML package*)
rdata = REvaluate["
                 skip.rows=1:2)[[1]]$Name -> file.list
filenames = DeleteCases[RFactorToVector[rdata], _Missing];

Lets look at the first 10 filenames:

In[98]:= filenames[[1 ;; 10]]

Out[98]= {"pdi20050101.wkly", "pdi20050108.wkly", "pdi20050115.wkly", \
"pdi20050122.wkly", "pdi20050129.wkly", "pdi20050205.wkly", \
"pdi20050212.wkly", "pdi20050219.wkly", "pdi20050222.wkly", \
share|improve this answer
+1. I do anticipate though more flames from Mac users, for the install.packages part, and they would be right ... – Leonid Shifrin Oct 2 '13 at 10:00
Completely! Is the Mac version of Mathematica any cheaper? I guess it should be if there is less functionality – WalkingRandomly Oct 2 '13 at 10:15
This was just very unfortunate - this was probably the single most important flaw that I didn't have the time to fix. I do hope to have the time to address this for the next version, though. – Leonid Shifrin Oct 2 '13 at 10:25
Re:RLink - thanks! Particularly great to hear it from you, given your very diverse background and interests in computational math tools and languages. There are tons of things it currently lacks, of course, but it has been a cool project. Re: PythonLink - not currently, alas, but possibly in the future. My own thinking is that the more links we have the better. That would allow one to exploit Mathematica as a hub for multi-language development, the role which IMO would suit it very well, and a direction which has been so far largely unexplored, particularly in the industry sector. – Leonid Shifrin Oct 2 '13 at 10:38
'fully working code in another language' - yep, totally agree. Mathematica can be a nice high-level interface to test and glue code written in a number of different languages, and add extra level of interactivity. – Leonid Shifrin Oct 2 '13 at 10:52

I like this way. It's similar to @Anon but with simpler Cases syntax:

files=Cases[source,{_,"href"->file_/;StringMatchQ[file,"*.*"]}:> file ,\[Infinity]];

And you could easily choose file format, using for example "*.gif" instead of "*.*"

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.