@weintek/filesystem
The built-in @weintek/filesystem module provides facilities for performing operations on file systems, such as creating directories and deleting files.
Note: Available when a Custom Model script runs on a local HMI.
Import
const fs = await import('@weintek/filesystem');Example: Create a directory and delete it
const fs = await import('@weintek/filesystem');
const disk = 'USB1';
const path = 'test';
fs.createDirectory(disk, path);
if (fs.exists(disk, path)) {
console.log('isDirectory =', fs.isDirectory(disk, path));
fs.remove(disk, path);
}Functions
All functions accept disk as their first parameter. Acceptable values for disk are: SD, USB1, USB2.
createDirectory(disk, path)
Creates a directory. Fails (returns false) if the parent directory does not exist.
Parameters
| Name | Type | Description |
|---|---|---|
disk | String | Target disk: SD, USB1, or USB2. |
path | String | Path of the directory to create. |
Returns: Boolean — true if created successfully.
exists(disk, path)
Tests whether a path exists.
Parameters
| Name | Type | Description |
|---|---|---|
disk | String | Target disk. |
path | String | Path to test. |
Returns: Boolean — true if the path exists.
isDirectory(disk, path)
Tests whether a path is a directory.
Parameters
| Name | Type | Description |
|---|---|---|
disk | String | Target disk. |
path | String | Path to test. |
Returns: Boolean — true if the path is a directory.
isRegularFile(disk, path)
Tests whether a path is a regular file.
Parameters
| Name | Type | Description |
|---|---|---|
disk | String | Target disk. |
path | String | Path to test. |
Returns: Boolean — true if the path is a regular file.
fileSize(disk, path)
Returns the file size in bytes.
Parameters
| Name | Type | Description |
|---|---|---|
disk | String | Target disk. |
path | String | File path. |
Returns: Number — File size in bytes.
readdir(disk, path)
Returns the names of files in the specified directory (excludes . and ..).
Parameters
| Name | Type | Description |
|---|---|---|
disk | String | Target disk. |
path | String | Directory path. |
Returns: String[] — Array of file names.
remove(disk, path)
Deletes a file or empty directory.
Parameters
| Name | Type | Description |
|---|---|---|
disk | String | Target disk. |
path | String | File or directory path. |
Returns: Boolean — true if deleted, false if it did not exist.
removeAll(disk, path)
Deletes a directory and all its subdirectories recursively.
Parameters
| Name | Type | Description |
|---|---|---|
disk | String | Target disk. |
path | String | Directory path. |
Returns: Number — The number of files and directories deleted.