List files in a directory

Hi,

I’m trying to create a template or shortcode that lists the files in a directory.

My particular use-case is that I’d like to have a page that lists the contents of a directory full of .wav and .mp3 files. Each file does not require its own page, so having the list template/shortcode display a direct link to the file is what I’m trying to do.

I’m having a bit of difficulty finding the right starting point to accomplish this. I’ve found documentation relating to .ResourceType / .MediaType (https://regisphilibert.com/blog/2018/01/hugo-page-resources-and-how-to-use-them/). However I’m having a hard time figuring it out and implementing it on my site.

Ideally, I’d like to figure this out on my own, as I’m mainly using Hugo as a hobby. If .ResourceType / .MediaType is indeed the correct direction to go in, please let me know. If not, all I ask is a point in the right direction, and I can eventually figure it out from there. Thank you!

I would go one of these routes:

(1) for a given page, walk its .Resources, then grab each .Permalink; or

(2) construct the URL yourself using local file templates https://gohugo.io/templates/files/#readout

2 Likes

a littel sample :wink:
I use this to make a linux doc (with sym link /etc to assets/etc)

shortlist.html shortcode

{{ $path     := .Get "path" }}
{{ $filepath := printf "assets/etc/%s" $path }}
{{ $files    := readDir $filepath }}
{{ $ext := default "-" (.Get "type" )}}
{{ if (fileExists $filepath)}}
<div class="pa2 mh4 ba">
{{- range $files }}
	{{ if (and (ne (substr .Name 0 1) "_") (not .IsDir)) }}{{ $f := resources.Get (printf "/etc/%s/%s" $path .Name)}}
		{{ $e := findRE "\\w+$" .Name }}
		{{ if (or ( eq $ext "-") (in $e $ext) )}}
			<a class="w-auto pr2" href={{$f.Permalink }} type=text/plain download>{{ .Name }}</a>
		{{end}}
	{{end}}
{{- end }}
</div>
{{end}}

how I call it

{{< lshortlist path="apache2/sites-enabled" type="conf">}}

2 Likes

Thank you so much! I’ll be testing this out shortly. I truly appreciate the time you took to help me, both ju52 and zwbetz. You guys are awesome!

So I tried your shortcode and couldn’t get it to function properly. After replacing your target directory with mine (static/audio), the page properly renders but does not list the files. Ju52, if you would be so kind, can you explain the string “/etc/%s/%s” found on line 10? I’m having a hard time understanding how to parse in my directory at that point. Would it be “/static/%s/%s” as to reflect my target directory?

My apologies for the lack of understanding, I’m in no way a programmer, ha.

$path ist the parameter from the shortcode
.Name comes from the RANGE operation, range goes over files, .Name ist simply the filename
Print puts all together

The call should be
{{< shortlist path=“apache2/sites-enabled” type=“conf”>}}

Forget to delete the first “l”

to go to static

{{ $filepath := printf “static/%s” $path }}

That blob @ju52 provided seemed promising but I couldn’t wrap my brain around it. I’m also very new to hugo, so doing anything confuses me.

I ended up coming up with this blob that might serve as a more basic starting point:

$ cat layouts/shortcodes/reportlist.html 
{{ $path := .Get "path" }}{{ $files := readDir $path }}
<ul class="reportlist">{{ range sort $files ".Name" "desc" }}{{ if not .IsDir }}{{ if ne .Name "index.md" }}
<li><a class="reportitem" href="/{{ replace $path "content/" ""}}{{ .Name }}">{{ .Name }}</a></li>
{{ end }}{{ end }}{{ end }}</ul>

Used with:

$ grep reportlist district_5/index.md 
{{< reportlist path="content/treasury/district_5/" >}}

Hi, what is your problem now?

You should put your files under /assets or /static. Hugo will copy all from static and all referenced files from assets.

PS:

  • I linked /assets/etc to /etc - to document my linux configuration
  • you could use [resources.GetMatch](https://gohugo.io/content-management/page-resources/#methods), if the files are in the same directory [/ subdirectory]