读取目录节点
概述
Read Directory节点读取指定目录的内容并输出一个文件名数组。该节点还可配置为递归读取目录、在输出中包含目录、过滤文件名、返回相对路径以及忽略某些文件或目录。
如果您正在使用Rivet SDK,此节点要求在运行图时上下文中有一个可用的原生API。该原生API负责提供读取目录内容的功能。
- 输入
- 输出
- 编辑器设置
输入项
Read Directory节点的唯一输入是可切换的编辑器设置。请参阅该部分了解可能的输入项。
输出
标题 | 数据类型 | 描述 | 备注 |
---|---|---|---|
Paths | string[] | An array of filenames in the specified directory. | If the directory does not exist or is not accessible, the output will be an array with one element containing the string (no such path) . |
Root Path | string | The path of the directory that was read. | The output will be the same as the Path input or the Path specified in the settings. |
编辑器设置
设置 | 描述 | 默认值 | 使用输入切换 | 输入数据类型 |
---|---|---|---|---|
Path | The path of the directory to read. | (required) | Yes | string |
Recursive | If enabled, the node will read directories recursively. | false | Yes | boolean |
Include Directories | If enabled, the node will include directories in the output. | false | Yes | boolean |
Filter Globs | An array of glob patterns to filter the filenames, comma-separated. | [] | Yes | string[] |
Relative | If enabled, the node will return relative paths. | false | Yes | boolean |
Ignores | An array of glob patterns to ignore certain files or directories, comma-separated. For example, to filter out node_modules , you can do **/node_modules/**/* . | [] | Yes | string[] |
示例1:读取目录内容
- 创建一个读取目录节点,并将
Path
设置为要读取的目录。 - 运行图表。Read Directory节点的
Paths
输出应包含指定目录中的文件名数组。
错误处理
如果在运行图形时上下文中没有可用的原生API,读取目录节点将会报错。如果没有提供Path
输入,它也会报错。
如果目录不存在或无法访问,Paths
输出将是一个包含单个元素的数组,该元素为字符串(no such path)
。
常见问题
问:什么是原生API?
A: 原生API是一个负责为节点提供原生功能的对象。它在图运行时被传递给上下文。原生API必须实现NativeApi
接口,该接口包含读取目录、读写文件以及其他原生操作的方法。更多信息请参阅API参考文档。
问:我可以递归读取目录吗?
A: 是的,您可以将Recursive
输入或设置设为true
来递归读取目录。
问:在输出中包含目录是什么意思?
A: 如果Include Directories
输入或设置被设为true
,那么Paths
输出将不仅包含文件名,还会包含目录名。例如,如果Path
输入设为/Users/username/Documents
,Paths
输出将包含类似file.txt
的文件名以及类似subdirectory
和subdirectory2
的目录名。
问:如何使用通配符进行过滤或忽略?
A: 全局模式(Glob patterns)是一种使用通配符过滤文件名的方法。例如,要过滤出所有.txt
文件,可以使用**/*.txt
。要过滤出node_modules
目录中的所有文件,可以使用**/node_modules/**/*
。
问:返回相对路径是什么意思?
A: 如果Relative
输入或设置设为true
,则Paths
输出将包含相对路径而非绝对路径。例如,若Path
输入设为/Users/username/Documents
,Paths
输出将包含类似file.txt
的文件名,而非/Users/username/Documents/file.txt
。