2015-06-26 06:23:37 -04:00
---
aliases:
- /doc/localfiles/
date: 2015-06-12
menu:
main:
parent: extras
next: /community/mailing-list
notoc: true
prev: /extras/urls
title: Traversing Local Files
weight: 110
---
## Traversing Local Files
2015-08-04 14:00:08 -04:00
Hugo includes a way to traverse local files.
2015-08-07 08:33:48 -04:00
This is done using the 'readDir' function.
2015-06-26 06:23:37 -04:00
2015-08-07 08:33:48 -04:00
## Using readDir
2015-06-26 06:23:37 -04:00
2015-08-07 08:33:48 -04:00
readDir takes a single string input that is relative to the root directory of the site. It returns an array of [os.FileInfo ](https://golang.org/pkg/os/#FileInfo )
2015-06-26 06:23:37 -04:00
2015-08-07 08:33:48 -04:00
Let's create a shortcode to build a file index with links using readDir.
2015-06-26 06:23:37 -04:00
'fileindex.html'
< table style = "width=100%" >
< th > Size in bytes< / th >
< th > Name< / th >
{{$dir := .Get "dir"}}
{{ $url := .Get "baseurl" }}
2015-08-07 08:33:48 -04:00
{{ $files := readDir $dir }}
2015-06-26 06:23:37 -04:00
{{ range $files }}
< tr >
< td > {{.Size}}< / td >
< td >
< a href = "{{$url}}{{.Name | urlize }}" > {{.Name}}< / a >
< / td >
2015-08-04 14:00:08 -04:00
< / tr >
2015-06-26 06:23:37 -04:00
{{ end }}
< / table >
Now lets use it to list the css files used on this site
{{< /* fileindex dir="static/css" baseurl="/css/" */>}}
Is rendered as:
{{< fileindex dir = "static/css/" baseurl = "/css/" > }}