🌳 Trees & Tree Parts Guide

July 25, 2025 Β· View on GitHub

Lua API Difficulty Version

πŸš€ Quick Start Guide for managing files as a one-dimensional list with LuaDoTheWorld Trees


πŸ“‹ What You'll Learn

  • βœ… How to create trees and tree parts
  • βœ… How to load existing directories as trees
  • βœ… How to modify files using tree operations
  • βœ… How to use map, count, and find operations
  • βœ… Understanding hardware operators (write, modify, remove)

πŸ› οΈ Prerequisites

  • LuaDoTheWorld installed and required in your script

🌱 Create a New Tree

Create files from scratch using tree parts:

local dtw = require("luaDoTheWorld/luaDoTheWorld")

local tree = dtw.newTree()
local a = tree.newTreePart_empty("tests/target/Tree/a.txt")
a.set_value("content of a")
a.hardware_write()

local b = tree.newTreePart_empty("tests/target/Tree/b.txt")
b.set_value("content of b")
b.hardware_write()

tree.commit()

πŸ“‚ Load Existing Directory as Tree

Work with files that already exist:

local dtw = require("luaDoTheWorld/luaDoTheWorld")

local tree = dtw.newTree_from_hardware("tests/target/test_dir")
-- Now you can work with all files in the directory

✏️ Modify All Files in a Tree

Change all .txt files to .py extension:

local dtw = require("luaDoTheWorld/luaDoTheWorld")

local tree = dtw.newTree_from_hardware("tests/target/test_dir")

tree.each(function(element)
    if element.path.get_extension() == "txt" then
        element.path.set_extension("py")
    end
    element.hardware_modify()
end)

tree.commit()

πŸ—ΊοΈ Map Tree to Custom Objects

Extract file information into a custom list:

local dtw = require("luaDoTheWorld/luaDoTheWorld")

local tree = dtw.newTree_from_hardware("tests/target/test_dir")

local result, size = tree.map(function(element)
    return {
        path = element.path.get_full_path(),
        content = element.get_value()
    }
end)

for i = 1, size do
    local current = result[i]
    print("========================")
    print("path:", current.path)
    print("content:", current.content)
end

πŸ” Count Files by Condition

Count how many .py files exist:

local dtw = require("luaDoTheWorld/luaDoTheWorld")

local tree = dtw.newTree_from_hardware("tests/target/test_dir")

local size = tree.count(function(element)
    if element.path.get_extension() == "py" then
        return true
    end
end)

print("Python files found:", size)

πŸ”§ Hardware Operations

πŸ“ Hardware Write

Creates new files, ignoring previous existence:

local dtw = require("luaDoTheWorld/luaDoTheWorld")

local tree = dtw.newTree_from_hardware("tests/target/test_dir")

local target = tree.find(function(element)
    if element.path.get_name() == "a.txt" then
        return true
    end
end)

target.path.set_name("new_name.txt")
target.hardware_write()  -- Creates new file, keeps old one
tree.commit()

πŸ”„ Hardware Modify

Renames/moves files, considering previous path:

local dtw = require("luaDoTheWorld/luaDoTheWorld")

local tree = dtw.newTree_from_hardware("tests/target/test_dir")

local target = tree.find(function(element)
    if element.path.get_name() == "a.txt" then
        return true
    end
end)

target.path.set_name("new_name.txt")
target.hardware_modify()  -- Renames the file
tree.commit()

πŸ—‘οΈ Hardware Remove

Deletes the specified tree part:

local dtw = require("luaDoTheWorld/luaDoTheWorld")

local tree = dtw.newTree_from_hardware("tests/target/test_dir")

local target = tree.find(function(element)
    if element.path.get_name() == "a.txt" then
        return true
    end
end)

target.hardware_remove()  -- Deletes the file
tree.commit()

πŸ“š Quick Reference

FunctionWhat it doesExample
dtw.newTree()Create empty treelocal tree = dtw.newTree()
dtw.newTree_from_hardware(path)Load directory as treedtw.newTree_from_hardware("folder")
tree.newTreePart_empty(path)Create new file parttree.newTreePart_empty("file.txt")
tree.each(function)Iterate over all partstree.each(function(element) ... end)
tree.map(function)Transform tree to listtree.map(function(element) ... end)
tree.count(function)Count matching partstree.count(function(element) ... end)
tree.find(function)Find specific parttree.find(function(element) ... end)
element.hardware_write()Create/overwrite fileelement.hardware_write()
element.hardware_modify()Rename/move fileelement.hardware_modify()
element.hardware_remove()Delete fileelement.hardware_remove()
tree.commit()Apply all changestree.commit()

πŸ’‘ Key Concepts

  • Tree: A collection of files managed as a single unit
  • Tree Part: Individual file within a tree
  • Hardware Write: Creates new files (ignores old ones)
  • Hardware Modify: Renames/moves files (considers old paths)
  • Hardware Remove: Deletes files
  • Commit: Applies all pending changes to disk

πŸ†˜ Need Help?

  • πŸ“– Check the main SDK documentation
  • πŸ” Look at other example scripts in the SDK
  • πŸ› Report issues on our GitHub repository

Footer