How to count the folders

Hello, i’m stuck on the question right now: How to count the folders itself (not the content in that folders)?

images/
images/folder-a/
images/folder-b/

Any advice is welcome, as always:-)

In which directory are the folders? Please show the file tree.

IΒ΄am in…

/content/my-section/my-page/images/...

and here I have some subfolders that I wish to count

/folder-a/
/folder-b/
/folder-c/

should give 3

So the complete file tree is:

content/
└── my-section/
    └── my-page/
        β”œβ”€β”€ images/
        β”‚   β”œβ”€β”€ folder-a/
        β”‚   └── folder-b/
        └── index.md

Correct?

Also, from which template do you wish to count them?

Yes, just like this.

i have a shortcode which is used in index.

And in the shortcode, is the path to images hardcoded, or are you passing the path as a shortcode argument?

I have a shortcode that counts the images within one single folder like so:

 {{< count-images folder="images/folder-a/*" >}}
{{- $folder := .Get "folder" | default "images/*"  -}}
{{- $prefix := .Get "prefix" | default "This collection contains "  -}}
{{- $einzahl := .Get "einzahl" | default " Image"  -}}
{{- $mehrzahl := .Get "mehrzahl" | default " Images"  -}}
{{- $suffix := .Get "suffix" | default ":" -}}

{{ $images := .Page.Resources.Match $folder }}

{{$prefix}} {{ len $images }} {{- if gt (len $images) 1 -}} {{$mehrzahl}} {{- else -}} {{$einzahl}} {{- end -}}{{$suffix}}

The new shortcode could also receive the path in this way:

 {{< count-folders folder="images/*" >}}
git clone --single-branch -b hugo-forum-topic-48948 https://github.com/jmooring/hugo-testing hugo-forum-topic-48948
cd hugo-forum-topic-48948
hugo server

The shortcode counts immediate descendant (child) directories. It is not recursive.

It also assumes a local (OS) file system (i.e., I don’t think this would work with mounted content).

2 Likes

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