Using a function to create a list of files from directory

I am creating a web page that will handle a lot of PDFs.

I am at the point where I have multiple folders in /static/ containing PDFs, for example /static/Folder1/Files1-x.pdf

This might be trivial but I am learning Hugo and markdown while using it and I have found the forums to be very helpful in learning. Is there a function or other way to take all these PDFs from a specific folder in /static/ and putting them into a list avaliable for download? Maybe a shortcode, but I haven’t really learned how to construct my own yet.

Thank you

This previous discussion will help you.

1 Like

That discussion was of no help; one person wrote a script that I do not understand at all and another person refered to readDir and readFile, which I have read about already but don’t know how to use.

I was hoping to get a little easier explanation on how to do what I need as I have very limited programming knowledge

Here is a simple example.

Create a shortcode layouts/shortcodes/pdf.html as follows.

{{ range readDir "static/Folder1" }}
  {{ if (findRE ".pdf$" .Name) }}
    <a href="Folder1/{{ .Name }}">{{ .Name }}</a>
  {{ end }}
{{ end }}

Use it in a Markdown file.

{{< pdf >}}

Thank you very much, this is very helpful.

I want to sort by file name, because right now for some reason all files are in random order and not the order that they are organized (but optimally alphabetically in my case). From what I understand I can use the sort function for this. I do not understand where functions like these should be entered in the code? Or if I am even using it correctly (because I am only getting errors when I try)

Another question I have is if it is possible to change the shortcode so that:

{{ range readDir "static/Folder1" }}

Is somehow “dynamic”? So for example, when using the shortcode, I would instead type

{{< pdf ../the folder I want to access/>}}

Instead of having it set on one specific folder.

Thank you for your help

Note that layouts/shortcodes/pdf.html is renamed to layouts/shortcodes/list-files.html for general use.

<!-- layouts/shortcodes/list-files.html -->

{{ $dir := .Get "dir" }}
{{ $regexp := .Get "regexp" }}

{{ range sort (readDir (delimit (slice "static" $dir) "/")) "Name" }}
  {{ if (findRE $regexp .Name) }}
    <a href="{{ $dir }}/{{ .Name }}">{{ .Name }}</a>
  {{ end }}
{{ end }}

The shortcode params can be passed like this.

{{< list-files dir="Folder1" regexp=".pdf$" >}}



Thank you very much for this, I will use it to better learn the syntax and how to create shortcodes.

However, the files did not get sorted! They appeared the same way as the previous shortcode.

Again, I am very thankful for this and it will help me learn

I do not know any reason for this. When I add desc like the following, it sorts the file list in reverse order. It looks to work.

{{ range sort (readDir (delimit (slice "static" $dir) "/")) "Name" "desc" }}

This topic was automatically closed 2 days after the last reply. New replies are no longer allowed.