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