# DirectoryList

List the contents of a directory.

Returns either an array, or a query depending on the {@code listInfo} argument.

The {@code listInfo} argument can be one of the following:

* {@code name} - Returns an array of the names of the items in the directory.
* {@code path} - Returns an array of the absolute paths of the items in the directory.
* {@code query} - Returns a query of the items in the directory containing the following fields:
  * {@code attributes} - The attributes of the item (R, W, X, H).
  * {@code dateLastModified} - The date the item was last modified.
  * {@code directory} - The directory containing the item.
  * {@code mode} - The mode of the item.
  * {@code name} - The name of the item.
  * {@code size} - The size of the item in bytes.
  * {@code type} - The type of the item (either "Dir" or "File").

The {@code filter} argument can be the following:

* A closure/lambda that takes a single argument (the path of the item) and returns a boolean. True to return it, false otherwise.

  ```
   DirectoryList( path: "/path/to/dir", filter: path -> path.endsWith(".txt") )
   
  ```
* A string that is a glob pattern: E.g. "\*.txt" to only return files with the .txt extension. Or you can use the {@code |} pipe to separate multiple patterns: E.g. "\*.txt|\*.csv" to return files with either the .txt or .csv extension.

## Method Signature

```
DirectoryList(path=[string], recurse=[boolean], listInfo=[string], filter=[any], sort=[string], type=[string])
```

### Arguments

| Argument   | Type      | Required | Description                                                                                                                                                                                      | Default |
| ---------- | --------- | -------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ | ------- |
| `path`     | `string`  | `true`   | The absolute path to the directory to list.                                                                                                                                                      |         |
| `recurse`  | `boolean` | `true`   | Whether to recurse into subdirectories or not. The default is false.                                                                                                                             | `false` |
| `listInfo` | `string`  | `false`  | The type of information to return. Valid values are "name", "path", and "query". The default is "path".                                                                                          | `path`  |
| `filter`   | `any`     | `false`  | A filter to apply to the listing. This can be a function that takes a single argument (the path of the item) and returns a boolean or a string that is a glob pattern. The default is no filter. |         |
| `sort`     | `string`  | `false`  | The sort order of the listing. Valid values are "name", "size", "date", and "type". The default is "name".You can also use `asc` or `desc` to specify the sort order. E.g. `sort: "name desc"`.  | `name`  |
| `type`     | `string`  | `false`  | The type of items to list. Valid values are "all", "file", and "dir". Default is "all".                                                                                                          | `all`   |

## Examples

### An array of files in this directory

[Run Example](https://try.boxlang.io/?code=eJxLLCpKrPRP88lPTsxxy8xJLVawVUjJLEpNLskvqvTJLC7RUEitKEjMSwlILMnQUFDS01dS0NRRSEvMKU7VUVDKS8xNBQpYcwEAo5oXPg%3D%3D)

```java
arrayOfLocalFiles = directoryList( expandPath( "./" ), false, "name" );

```

Result: \[.DS\_Store, .ortus, Application.bx, MyDestinationDirectory, Page.bx, assets, bifs, components, compressed\_test.txt.gz, example.bxm, example.bxm, filepath, images, index.bxm, myNewFileName.txt, new, new\_directory, server.json, setup\_db.sql, some, test.txt, testcase.txt]

### A query of files in this directory sorted by date last modified

[Run Example](https://try.boxlang.io/?code=eJwrLE0tqvRPc8vMSS1WsFVIySxKTS7JL6r0ySwu0VBIrShIzEsJSCzJ0FBQ0tNXUtDUUUhLzClO1VFQKgTpVAIyQNglsSTVJ7G4xDc%2FJTMtMzVFwcU12Bmo3JoLAELdHpE%3D)

```java
queryOfFiles = directoryList( expandPath( "./" ), false, "query", "", "DateLastModified DESC" );

```

### An array of files in the temp directory

Including sub-directories and as an array containing full paths

[Run Example](https://try.boxlang.io/?code=eJxLLCpKrPRPC0nNLXDLzEktVrBVSMksSk0uyS%2Bq9MksLtFQUNLTV9JRKCkqTVXQtOYCAKoOD88%3D)

```java
arrayOfTempFiles = directoryList( "./", true );

```

### Filter files with closure

Pass a closure instead of a string as `filter` param

[Run Example](https://try.boxlang.io/?code=eJwljUsKg0AQRNfxFI0rA8NcIBiQRFf5QG7Qji1O0Bnp7gQl5O4ZyaaKgno8ZMb13jd%2BVGLqUpNACZ1nchp5vXjRAnKbG%2BhxFDKQB5wozQKqsMKMOsAeyiN8sh2TvjjAxjQ%2BdLd4QqFEV%2FM8eofqY7DtYji2UcXqokaI38T2KTGYHt%2FepUcKYwdF50jEPOrqfK3t1CXlX3bIvlv8AAO8Pew%3D)

```java
arrayOfFilteredFiles = directoryList( ".", false, "name", ( Any path ) => {
	return ListFindNoCase( "Application.bx,robots.txt,server.json,favicon.ico,.htaccess,README.md", path );
} );

```

Result: \[]

### Additional Examples

[Run Example](https://try.boxlang.io/?code=eJxLySxKTS7JL6r0ySwu0VBQ0i9LLNJPSSxJVNJRSEvMKU7VUVAqSCzJAHI1FBzzKhVAHAVNBVs7hWouzqLUktKiPIXEovTS3NS8kmK9AMcQD72MxOLg0rS0zAqgeXo5%2BelKCprWXLUgAgDHRiJT)

```java
directoryList( "/var/data", false, "path", ( Any path ) => {
	return arguments.PATH.hasSuffix( ".log" );
} );

```

Result: \[]

## Related

* [ContractPath](/boxlang-language/reference/built-in-functions/io/contractpath.md)
* [CreateTempDirectory](/boxlang-language/reference/built-in-functions/io/createtempdirectory.md)
* [CreateTempFile](/boxlang-language/reference/built-in-functions/io/createtempfile.md)
* [DirectoryCopy](/boxlang-language/reference/built-in-functions/io/directorycopy.md)
* [DirectoryCreate](/boxlang-language/reference/built-in-functions/io/directorycreate.md)
* [DirectoryDelete](/boxlang-language/reference/built-in-functions/io/directorydelete.md)
* [DirectoryExists](/boxlang-language/reference/built-in-functions/io/directoryexists.md)
* [DirectoryMove](/boxlang-language/reference/built-in-functions/io/directorymove.md)
* [DirectoryRename](/boxlang-language/reference/built-in-functions/io/directoryrename.md)
* [ExpandPath](/boxlang-language/reference/built-in-functions/io/expandpath.md)
* [FileAppend](/boxlang-language/reference/built-in-functions/io/fileappend.md)
* [FileClose](/boxlang-language/reference/built-in-functions/io/fileclose.md)
* [FileCopy](/boxlang-language/reference/built-in-functions/io/filecopy.md)
* [FileDelete](/boxlang-language/reference/built-in-functions/io/filedelete.md)
* [FileExists](/boxlang-language/reference/built-in-functions/io/fileexists.md)
* [FileGetMimeType](/boxlang-language/reference/built-in-functions/io/filegetmimetype.md)
* [FileInfo](/boxlang-language/reference/built-in-functions/io/fileinfo.md)
* [FileIsEOF](/boxlang-language/reference/built-in-functions/io/fileiseof.md)
* [FileMove](/boxlang-language/reference/built-in-functions/io/filemove.md)
* [FileOpen](/boxlang-language/reference/built-in-functions/io/fileopen.md)
* [FileRead](/boxlang-language/reference/built-in-functions/io/fileread.md)
* [FileReadBinary](/boxlang-language/reference/built-in-functions/io/filereadbinary.md)
* [FileReadLine](/boxlang-language/reference/built-in-functions/io/filereadline.md)
* [FileSeek](/boxlang-language/reference/built-in-functions/io/fileseek.md)
* [FileSetAccessMode](/boxlang-language/reference/built-in-functions/io/filesetaccessmode.md)
* [FileSetAttribute](/boxlang-language/reference/built-in-functions/io/filesetattribute.md)
* [FileSetLastModified](/boxlang-language/reference/built-in-functions/io/filesetlastmodified.md)
* [FileSkipBytes](/boxlang-language/reference/built-in-functions/io/fileskipbytes.md)
* [FileWrite](/boxlang-language/reference/built-in-functions/io/filewrite.md)
* [FileWriteLine](/boxlang-language/reference/built-in-functions/io/filewriteline.md)
* [GetCanonicalPath](/boxlang-language/reference/built-in-functions/io/getcanonicalpath.md)
* [GetDirectoryFromPath](/boxlang-language/reference/built-in-functions/io/getdirectoryfrompath.md)
* [GetFileInfo](/boxlang-language/reference/built-in-functions/io/getfileinfo.md)
* [getTempFile](/boxlang-language/reference/built-in-functions/io/gettempfile.md)
* [PropertyFile](/boxlang-language/reference/built-in-functions/io/propertyfile.md)


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://boxlang.ortusbooks.com/boxlang-language/reference/built-in-functions/io/directorylist.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
