GetFiles and Folders - examples Last updated: 20. Feb 2023
Examples for using the functions above. Also sorting documents or folders on custom properties.
ListFiles(path, wildcard)
Fastest way to retreive files:
#let files = docly.listFiles("/")#
#for (let file of files) {#
<div>#file.Name#</div>
#}#
Returns following structure, see link:
https://developers.docly.net/JavaScript/Filesystem-functions/ListFiles
GetFiles(path, wildcard, depth, mode)
Parameter name | Type | Description |
---|---|---|
path | string | Path to the folder to get items from. |
wildcard (optional) | string | A file system wildcard. For example: "*.pdf". Default is "*", meaning all files. |
depth (optional) | int | How many extra levels of sub folders to search. Default is 0 meaning that it will only search the specified folder. |
mode (optional) | enum | Full = All properties (slowest) Medium = A lot of properties (OK speed) Min = Minimum (fastest) "Medium" is default. |
Example:
<ul>
#for(let file of docly.getFiles(request.folderpath)) {#
<li>
<a href="#urlpathencode(trim(replace(replace(file.Url,' ','-'),'##','%23'),'/'))#">
#file.filename#
</a>
</li>
#}#
</ul>
Example 2:
<div>
#for (let file of docly.getFiles('/')) {#
<div>
#file.filename# - Filename<br />
#file.Title# - Title<br/>
#file.file# - Is file<br/>
#file.folderpath# - Folder path<br/>
#file.Url# - Show file url<br/>
#file.opendoc# - Edit doc url<br/>
#file.DoclyType# - Document type name (string)<br/>
#file.DoclyTypeId# - Document type ID (number)<br/>
#file.FormName# - Schema type name (string)<br/>
#file.FormId# - Schema type ID (number)<br/>
#file.CreatedTime# - DateTime when document was created<br/>
#file.LastModified# - DateTime when document was last changed<br/>
#file.LastModifiedBy# - Username (email) of who last changed the document<br/>
#file.DoclyId# - Folder or document Id<br/>
#file.Filesize# - File size in bytes<br/>
<!-- Read any page property fields directly: -->
#file.Property1# - One of your custom document fields<br />
</div>
#}#
</div>
<div>
#for (let file of docly.getFiles('/', "*.pdf")) {#
... only pdf files will be listed ...
#}#
</div>
<div>
#for (let file of docly.getFiles('/', "*.pdf", 2)) {#
... pdf files will be listed going down 2 extra levels of sub folders ...
#}#
</div>
GetFolders(path, wildcard, depth)
Same parameters as for "GetFiles".
#for(let folder of docly.getFolders(request.folderpath)) {#
<div>
#folder.Name# - folder name<br />
#folder.Url# - folder path<br />
#folder.DoclyId# - folder id<br/>
<!-- Read any folder property fields directly: -->
#folder.Property1# - One of your custom document fields<br />
</div>
#}#
Example - Listing folders and files (with sorting)
To be able to sort folders you must first set a folder schema type (right click folder and click properties, select type then enter value in setting tab).
<!-- In the example below we are sorting on a field called "Order" for folders and documents. You will have to setup this yourself in your schemas too. -->
<div>
#for(let folder of sort(docly.getfolders(request.folderpath), 'Order')) {#
<div>
Folder: #folder.Name#
Image: <img src="#folder.Url#/#folder.Image#" title="Test" />
</div>
<ul>
#for(let file of sort(docly.getfiles(request.folderpath + '/' + file.Name), 'Order', 'CInt')) {#
<li>
<img src="#file.Url#/#file.Logo#/100x100x0/#file.MyTitle#.jpg" alt="#file.MyTitle#" />
<a href="#urlpathencode(trim(replace(replace(file.Url,' ','-'), '##', '%23'), '/'))#">
#file.filename#
</a>
</li>
#}#
</ul>
#}#
</div>
For folders using the schema "Published folder" sort by the field "SortOrder":
.... #for (let folder of sort(getfolders('MyFolder'), 'SortOrder', 'CInt')) {# ...
Example - List documents from multiple folders
Join contents of multiple folders:
<div >
#let list = docly.getFiles("Folder1").concat(docly.getFiles("Folder2"))# (Concat two folders)
#let list = sortdesc(list, "Publisert"))# (Sort the list)
#let list = take(list, 5)# (Take 5 first items in list)
#for(let item of list) {#
#item.Name# - folder name<br />
...
#}#
</div>
You would probably want to sort the list as well.