A way to traverse local files?

Hi guys,
I am trying to host a gallery site with a bunch of images using Hugo. Thanks to the v0.14 update now I can organize images in the content folder.
What I want to do is automatically traverse all the image files in a folder and grab their file names (or maybe other info extracted from EXIF) and process them in a post. And I’m stucking with howto “traverse”, or in another word, get a list of all the names of these image files.
Surely a possible workaround is I write a script and loop through these files beforehand which results in a json file containing all these names I need and feed to Hugo. But think it is an ugly solution which needs extra effort instead of letting Hugo automatically do the things I want.
Do you guys ever run into similar problem or have any ideas? Or maybe I’m missing some Hugo features which can easily handle this? Thanks in advance.

1 Like

@bep That’s great, thanks for the info :slight_smile:

The thread that is linked to is not active anymore (the github project stated is now deleted).

Perhaps better to clarify here how this works? I personally want to know, but also i feel it is beneficial having this info here also in this forum controlled by Hugo.

There is this:

"Thanks to the v0.14 update now I can organize images in the content folder. "

But i am not sure what this means. I did not know there was such an obvious chance in this version.

looks like the branch got deleted from his fork

What is asked for in this heading was supported by the PR.

The PR was retracted when I asked for some changes/improvements/fixes. That is fine. If someone want this functionality, I guess the fastest way is to implement self and send a PR. But it has to be on a quality level where I do not have to spend a lot of time on this forum answering questions about why it doesn’t work on Windows etc.

hi, @lynxiayel

i was the one that wrote the mentioned getFileList command.

As @bep mentioned I retracted the PR after his comments that were completely appropriate. The pull request didn’t meet the required standard that has been set and at the time I didn’t have the time or experience using go to address the suggestions raised.

I have made a branch of hugo that includes the command renamed to ReadDir

The ReadDir function simply returns an array of type FileInfofrom which you can access the name.

I use it to generate links from a folder containing photos from within the static directory which is turned into a blueimp gallery

I insert a gallery using a partial “gallery.html” which contains the following which generates a html tag <a> for each filename.

{{$dir := .Scratch.Get "dir" }}
{{$url := .Scratch.Get "url" }}
{{ $images := ReadDir  $dir }}
{{ range $images }}
		<a href="{{$url }}/{{.Name | urlize }}">
	{{ end }}

at the moment I pass the partial dir=“static/photos” and url=“photos” since the contents of static get copied across to the public directory.
At the moment the ReadDir function will only allow you to traverse a subdirectory of the site root directory.

Feel free to adapt and refine it for inclusion into the official repo.

To see it in action visit russelloliver.com.au

Hi @mikeaja, by “Thanks to the v0.14 update now I can organize images in the content folder.” I am referring to this issue. Before v0.14, non md files in your content folder are ignored, and images have to be put in the static folder.

And for the answer to my question about “traverse local files”, please refer to @rustyoz’s answer.

New pull request for the command with docs.

Great, I think I’ll need exactly this on my project, so thanks ! I hope it will be merged soon. :slight_smile:

Mirror of gohugo.io running the ReadDir branch with docs.

@rustyoz one thing missing is the list of attributes you get, like name and size. Is there more ?

the returned list is https://golang.org/pkg/os/#FileInfo

1 Like

Looks exactly like what I need for my project, too! Hope to see it in a release soon…

1 Like