# Iterate over files in a directory

**URL:** <https://zola.discourse.group/t/iterate-over-files-in-a-directory/899>\
**Category:** Support\
**Created:** [May 10, 2021, 8:48pm UTC](https://zola.discourse.group/t/iterate-over-files-in-a-directory/899 "2021-05-10T20:48:26Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![akiross](https://yyz2.discourse-cdn.com/free1/user_avatar/zola.discourse.group/akiross/32/503_2.png) [@akiross](https://zola.discourse.group/u/akiross)\
**Post date:** [May 10, 2021, 8:48pm UTC](https://zola.discourse.group/t/iterate-over-files-in-a-directory/899/1 "2021-05-10T20:48:26Z")

</div>

Hello,  
I’m porting a website to zola and I was wondering if there is a way to iterate over the files of a directory in the source tree (e.g. in `static`).

The use case is to implement a statically generated image gallery: a directory will contain all the images of the gallery, with a loop I want to iterate over all the files of that directory, produce the thumbnails and link to the full resolution image.

Is it possible to do so? I couldn’t find a way.  
Thanks!

---

<div class="post-metadata">

**Author:** ![akiross](https://yyz2.discourse-cdn.com/free1/user_avatar/zola.discourse.group/akiross/32/503_2.png) [@akiross](https://zola.discourse.group/u/akiross)\
**Post date:** [May 10, 2021, 9:12pm UTC](https://zola.discourse.group/t/iterate-over-files-in-a-directory/899/2 "2021-05-10T21:12:34Z")

</div>

Just as an information, the solution I’m using so far is to use `load_data` with a file generated automatically.

This works, but of course the problems are"

- the necessity of an external script and building phase and
- most importantly, the fact that information regarding the galleries is sparse (or the script is smart enough).

Having something like hugo does (`readDir` IIRC) would be nicer I think.

---

<div class="post-metadata">

**Author:** ![keats](https://avatars.discourse-cdn.com/v4/letter/k/34f0e0/32.png) [@keats](https://zola.discourse.group/u/keats)\
**Post date:** [May 11, 2021, 12:41pm UTC](https://zola.discourse.group/t/iterate-over-files-in-a-directory/899/3 "2021-05-11T12:41:18Z")

</div>

It’s not really possible right now. I’m going to work on [Unify the usage of `path` parameter in functions · Issue #977 · getzola/zola · GitHub](https://github.com/getzola/zola/issues/977#issuecomment-837226868) next and an equivalent to `readDir` could make sense to add.  
Would you only use it to read dirs from static?

---

<div class="post-metadata">

**Author:** ![akiross](https://yyz2.discourse-cdn.com/free1/user_avatar/zola.discourse.group/akiross/32/503_2.png) [@akiross](https://zola.discourse.group/u/akiross)\
**Post date:** [May 11, 2021, 6:10pm UTC](https://zola.discourse.group/t/iterate-over-files-in-a-directory/899/4 "2021-05-11T18:10:49Z")

</div>

Well, not necessary only from `static` and actually for the same use case (gallery) being able to access co-located data would be great, too.

In hugo I used to have shortcodes for adding a gallery to a page, so it also makes sense, to me, to have a directory that contains the page for a topic and the gallery/galleries to be displayed in that page.

---

<div class="post-metadata">

**Author:** ![keats](https://avatars.discourse-cdn.com/v4/letter/k/34f0e0/32.png) [@keats](https://zola.discourse.group/u/keats)\
**Post date:** [May 11, 2021, 6:17pm UTC](https://zola.discourse.group/t/iterate-over-files-in-a-directory/899/5 "2021-05-11T18:17:20Z")

</div>

You can already do that in the content with [colocated assets](https://www.getzola.org/documentation/content/overview/#asset-colocation) and a gallery example: [Image processing | Zola](https://www.getzola.org/documentation/content/image-processing/#creating-picture-galleries)

---

<div class="post-metadata">

**Author:** ![akiross](https://yyz2.discourse-cdn.com/free1/user_avatar/zola.discourse.group/akiross/32/503_2.png) [@akiross](https://zola.discourse.group/u/akiross)\
**Post date:** [May 11, 2021, 6:29pm UTC](https://zola.discourse.group/t/iterate-over-files-in-a-directory/899/6 "2021-05-11T18:29:44Z")

</div>

Oh wow! I didn’t know that 🙂 I could use that, I don’t have a compelling need to place images in `static`.

This being said, having a similar function to access static could be useful.

Thank you!

---

<div class="post-metadata">

**Author:** ![dossalab](https://yyz2.discourse-cdn.com/free1/user_avatar/zola.discourse.group/dossalab/32/1134_2.png) [@dossalab](https://zola.discourse.group/u/dossalab)\
**Post date:** [December 10, 2023, 7:53pm UTC](https://zola.discourse.group/t/iterate-over-files-in-a-directory/899/7 "2023-12-10T19:53:50Z")

</div>

Sorry to bump this, but I found this topic on Google and I’m wondering if such function was added? I wanted to create a gallery on my index page, and I currently have to list all the stuff myself. Using assets doesn’t seem to work reliably since index is a section, not a page

---

<div class="post-metadata">

**Author:** ![keats](https://avatars.discourse-cdn.com/v4/letter/k/34f0e0/32.png) [@keats](https://zola.discourse.group/u/keats)\
**Post date:** [December 12, 2023, 8:46am UTC](https://zola.discourse.group/t/iterate-over-files-in-a-directory/899/8 "2023-12-12T08:46:40Z")

</div>

No functions like that has been added.
