Core\FileTransfer\Connection::walk
$connection->walk(string $folder = ""): Core\FileTransfer\Walk$connection->walk( string $folder = "", ): Core\FileTransfer\Walk
Gives every file and folder below one folder of a server, one at a time, in a foreach loop.
Each item is a Core\FileTransfer\Entry. A folder comes first, then everything in it, and the items of one folder come in name order. relativePath() gives the path below the folder you started at. An empty string starts at the block's root. Each step of the loop reads at most one folder, so a large tree is not read into memory at once.
walk works the same way over SFTP, FTPS, FTP and a directory block (a folder on disk that replaces the server). A link is given as one item. The walk does not list what is in the link's target.
Good to know: a folder more than 64 levels below the start folder throws an IOError. A folder that is deleted while the loop runs also throws an IOError, when the loop reaches it.
Parameters
$folderstringdefault ""The folder, relative to the block'sroot. An empty string is therootitself.
Return value
Returns Core\FileTransfer\Walk. One Core\FileTransfer\Entry per item. A folder comes first, then everything in it. The items of one folder come in name order.
Errors
RuntimeErrorThe path starts with/, has an empty part or a..part, or has a NUL, CR or LF character.IOErrorThe folder$folderdoes not exist, or the server does not answer. The loop also throws it when it reaches a folder that is more than 64 levels below$folderor no longer exists.LogicErrorThe connection is closed.
Changelog
| Version | Change |
|---|---|
0.0.6 | Core\FileTransfer\Connection lists, reads, writes, moves and removes files on the server, with the method names of Core\IO. write first writes a temporary .part file and then renames it, so the server never has half a file. readStream reads a file in chunks of at most 64 KiB, so a large file is never in memory at once. |
Examples
The examples below run with this configuration file.
# The `partner` block reads the folder `partner` beside this file. It needs no server.[file_transfer.partner]directory = "partner"
# The program may open the `partner` block.[capabilities.file_transfer]connect = ["partner"]
# A `directory` block also needs the right to read its folder.[capabilities.fs]read = ["partner"]Every file below a folder
<?nvs// Shows how to list every file and folder below one folder of a partner's server.
use Core\FileTransfer;use Core\FileTransfer\Entry;
// `open` takes the name of the block `[file_transfer.partner]` in `nvs.toml`.var $partner = FileTransfer::open('partner');
// A folder comes first, then everything in it.// `relativePath` is the path below `reports`, such as `2026/01/sales.csv`.foreach ($partner->walk('reports') as Entry $entry) { echo $entry->relativePath(), $entry->isDirectory() ? '/' : '', "\n";}2026/2026/01/2026/01/returns.csv2026/01/sales.csv2026/02/2026/02/sales.csvreadme.txtThe size of a folder tree
<?nvs// Shows how to count the files and folders below a folder and add up the file sizes.
use Core\FileTransfer;use Core\FileTransfer\Entry;
var $partner = FileTransfer::open('partner');
uint $files = 0;uint $folders = 0;uint $bytes = 0;// An empty string is the block's `root`, so this walks the whole server folder.foreach ($partner->walk() as Entry $entry) { if ($entry->isDirectory()) { $folders = $folders + 1; } else { uint $size = $entry->size() ?? 0; $files = $files + 1; $bytes = $bytes + $size; }}
// This prints "4 files in 4 folders, 104 bytes".echo $files, ' files in ', $folders, ' folders, ', $bytes, " bytes\n";4 files in 4 folders, 104 bytesFind every sales report
<?nvs// Shows a monthly script that finds every sales report below a folder, in any month.
use Core\FileTransfer;use Core\FileTransfer\Entry;
var $partner = FileTransfer::open('partner');
// The items of one folder come in name order, so the months come in order too.uint $found = 0;foreach ($partner->walk('reports') as Entry $entry) { if ($entry->isFile() && $entry->name() == 'sales.csv') { echo $entry->path(), "\n"; $found = $found + 1; }}echo $found, " sales reports\n";reports/2026/01/sales.csvreports/2026/02/sales.csv2 sales reports