feat: add wasm build targets, profile hooks, os_demo example, and restructure manual source
- Add `wasm`, `wasm-clean`, and `install-emscripten` phony targets to Makefile for WebAssembly cross-compilation
- Insert `WREN_PROFILE_ENTER`/`WREN_PROFILE_EXIT` macros in `wren_vm.h` and `wren_vm.c` to support optional runtime profiling via `WREN_PROFILE_ENABLED`
- Create `example/os_demo.wren` demonstrating Platform, Process, and conditional exit usage
- Update `example/regex_demo.wren` to import `Match` and exercise Match object properties, groups, and `matchAll`
- Remove static HTML manual pages (`base64.html`, `dns.html`, `json.html`) and replace with structured `manual_src/` directory containing Jinja2 templates, YAML metadata, and content pages
- Expand `README.md` with build targets table, manual building instructions, source layout, and guide for adding new module documentation
2026-01-26 05:12:14 +01:00
{# retoor < retoor @ molodetz . nl > #}
{% extends 'page.html' %}
{% set page_title = "File Operations" %}
{% set breadcrumb = [{"url": "howto/index.html", "title": "How-To Guides"}, {"title": "File Operations"}] %}
{% set prev_page = {"url": "howto/regex-patterns.html", "title": "Regex Patterns"} %}
{% set next_page = {"url": "howto/async-operations.html", "title": "Async Operations"} %}
{% block article %}
feat: add example scripts for argparse, bytes, dataset, html, markdown, uuid, wdantic, and web chat modules
Add comprehensive demo scripts showcasing the usage of multiple Wren modules including argument parsing, byte manipulation, in-memory dataset operations, HTML encoding/slugification, markdown-to-HTML conversion, UUID generation/validation, schema validation with wdantic, and a full web chat application with REST endpoints.
2026-01-24 23:40:22 +01:00
< h1 > File Operations< / h1 >
< h2 > Read Entire File< / h2 >
< pre > < code > import "io" for File
var content = File.read("document.txt")
System.print(content)< / code > < / pre >
< h2 > Write to File< / h2 >
< pre > < code > import "io" for File
File.write("output.txt", "Hello, World!")
System.print("File written!")< / code > < / pre >
< h2 > Append to File< / h2 >
< pre > < code > import "io" for File
var existing = File.exists("log.txt") ? File.read("log.txt") : ""
File.write("log.txt", existing + "New line\n")< / code > < / pre >
< h2 > Check if File Exists< / h2 >
< pre > < code > import "io" for File
if (File.exists("config.txt")) {
System.print("Config found")
var content = File.read("config.txt")
} else {
System.print("Config not found, using defaults")
}< / code > < / pre >
< h2 > Get File Size< / h2 >
< pre > < code > import "io" for File
var size = File.size("data.bin")
System.print("File size: %(size) bytes")< / code > < / pre >
< h2 > Copy File< / h2 >
< pre > < code > import "io" for File
File.copy("source.txt", "destination.txt")
System.print("File copied!")< / code > < / pre >
< h2 > Rename/Move File< / h2 >
< pre > < code > import "io" for File
File.rename("old_name.txt", "new_name.txt")
System.print("File renamed!")
File.rename("file.txt", "subdir/file.txt")
System.print("File moved!")< / code > < / pre >
< h2 > Delete File< / h2 >
< pre > < code > import "io" for File
if (File.exists("temp.txt")) {
File.delete("temp.txt")
System.print("File deleted!")
}< / code > < / pre >
< h2 > List Directory Contents< / h2 >
< pre > < code > import "io" for Directory
var files = Directory.list(".")
for (file in files) {
System.print(file)
}< / code > < / pre >
< h2 > Check if Directory Exists< / h2 >
< pre > < code > import "io" for Directory
if (Directory.exists("data")) {
System.print("Directory found")
} else {
System.print("Directory not found")
}< / code > < / pre >
< h2 > Create Directory< / h2 >
< pre > < code > import "io" for Directory
if (!Directory.exists("output")) {
Directory.create("output")
System.print("Directory created!")
}< / code > < / pre >
< h2 > Delete Empty Directory< / h2 >
< pre > < code > import "io" for Directory
Directory.delete("empty_folder")
System.print("Directory deleted!")< / code > < / pre >
< h2 > Read File Line by Line< / h2 >
< pre > < code > import "io" for File
var content = File.read("data.txt")
var lines = content.split("\n")
for (line in lines) {
if (line.count > 0) {
System.print(line)
}
}< / code > < / pre >
< h2 > Process Files in Directory< / h2 >
< pre > < code > import "io" for File, Directory
var files = Directory.list("./data")
for (filename in files) {
if (filename.endsWith(".txt")) {
var path = "./data/%(filename)"
var content = File.read(path)
System.print("%(filename): %(content.count) chars")
}
}< / code > < / pre >
< h2 > Recursive Directory Listing< / h2 >
< pre > < code > import "io" for File, Directory
var listRecursive = Fn.new { |path, indent|
var items = Directory.list(path)
for (item in items) {
var fullPath = "%(path)/%(item)"
System.print("%(indent)%(item)")
if (Directory.exists(fullPath)) {
listRecursive.call(fullPath, indent + " ")
}
}
}
listRecursive.call(".", "")< / code > < / pre >
< h2 > Find Files by Extension< / h2 >
< pre > < code > import "io" for File, Directory
var findByExtension = Fn.new { |path, ext|
var results = []
var items = Directory.list(path)
for (item in items) {
var fullPath = "%(path)/%(item)"
if (Directory.exists(fullPath)) {
var subResults = findByExtension.call(fullPath, ext)
for (r in subResults) results.add(r)
} else if (item.endsWith(ext)) {
results.add(fullPath)
}
}
return results
}
var wrenFiles = findByExtension.call(".", ".wren")
for (file in wrenFiles) {
System.print(file)
}< / code > < / pre >
< h2 > Read JSON Configuration< / h2 >
< pre > < code > import "io" for File
import "json" for Json
var loadConfig = Fn.new { |path, defaults|
if (!File.exists(path)) {
return defaults
}
var content = File.read(path)
var config = Json.parse(content)
for (key in defaults.keys) {
if (!config.containsKey(key)) {
config[key] = defaults[key]
}
}
return config
}
var config = loadConfig.call("config.json", {
"port": 8080,
"debug": false
})
System.print("Port: %(config["port"])")< / code > < / pre >
< h2 > Save JSON Configuration< / h2 >
< pre > < code > import "io" for File
import "json" for Json
var config = {
"database": "app.db",
"port": 8080,
"debug": true
}
File.write("config.json", Json.stringify(config, 2))
System.print("Configuration saved!")< / code > < / pre >
< h2 > Create Backup Copy< / h2 >
< pre > < code > import "io" for File
import "datetime" for DateTime
var backup = Fn.new { |path|
if (!File.exists(path)) {
System.print("File not found: %(path)")
return null
}
var timestamp = DateTime.now().format("\%Y\%m\%d_\%H\%M\%S")
var backupPath = "%(path).%(timestamp).bak"
File.copy(path, backupPath)
System.print("Backup created: %(backupPath)")
return backupPath
}
backup.call("important.txt")< / code > < / pre >
< h2 > Read User Input< / h2 >
< pre > < code > import "io" for Stdin
System.write("Enter your name: ")
var name = Stdin.readLine()
System.print("Hello, %(name)!")< / code > < / pre >
< h2 > Interactive Menu< / h2 >
< pre > < code > import "io" for Stdin
System.print("Select an option:")
System.print("1. Option A")
System.print("2. Option B")
System.print("3. Exit")
System.write("Choice: ")
var choice = Stdin.readLine()
if (choice == "1") {
System.print("You selected Option A")
} else if (choice == "2") {
System.print("You selected Option B")
} else if (choice == "3") {
System.print("Goodbye!")
}< / code > < / pre >
< h2 > Safe File Operations with Error Handling< / h2 >
< pre > < code > import "io" for File
var safeRead = Fn.new { |path|
var fiber = Fiber.new { File.read(path) }
var result = fiber.try()
if (fiber.error) {
System.print("Error reading %(path): %(fiber.error)")
return null
}
return result
}
var content = safeRead.call("maybe_exists.txt")
if (content) {
System.print("Content: %(content)")
}< / code > < / pre >
< h2 > Calculate Directory Size< / h2 >
< pre > < code > import "io" for File, Directory
var dirSize = Fn.new { |path|
var total = 0
var items = Directory.list(path)
for (item in items) {
var fullPath = "%(path)/%(item)"
if (Directory.exists(fullPath)) {
total = total + dirSize.call(fullPath)
} else if (File.exists(fullPath)) {
total = total + File.size(fullPath)
}
}
return total
}
var size = dirSize.call(".")
System.print("Total size: %(size) bytes")< / code > < / pre >
< div class = "admonition tip" >
< div class = "admonition-title" > See Also< / div >
feat: add async/await keywords, Num/String extensions, and Jinja markdown example
Add `async` and `await` token support to the Wren compiler with scheduler resolution logic, extend `Num` with `e` constant, hyperbolic trig, base conversion, and new methods (`isZero`, `gcd`, `lcm`, `digits`, etc.), extend `String` with `lower`, `upper`, `capitalize`, `title`, and regenerate `wren_core.wren.inc`. Include `Makefile` for multi-platform builds, rewrite `README.md` with updated build instructions, and add `example/await_demo.wren` and `example/jinja_markdown.wren` demonstrating new features.
2026-01-25 04:58:39 +01:00
< p > For full API documentation, see the < a href = "../api/io.html" > IO module reference< / a > . For object-oriented path manipulation with glob, walk, and tree operations, see the < a href = "../api/pathlib.html" > pathlib module reference< / a > .< / p >
feat: add example scripts for argparse, bytes, dataset, html, markdown, uuid, wdantic, and web chat modules
Add comprehensive demo scripts showcasing the usage of multiple Wren modules including argument parsing, byte manipulation, in-memory dataset operations, HTML encoding/slugification, markdown-to-HTML conversion, UUID generation/validation, schema validation with wdantic, and a full web chat application with REST endpoints.
2026-01-24 23:40:22 +01:00
< / div >
feat: add wasm build targets, profile hooks, os_demo example, and restructure manual source
- Add `wasm`, `wasm-clean`, and `install-emscripten` phony targets to Makefile for WebAssembly cross-compilation
- Insert `WREN_PROFILE_ENTER`/`WREN_PROFILE_EXIT` macros in `wren_vm.h` and `wren_vm.c` to support optional runtime profiling via `WREN_PROFILE_ENABLED`
- Create `example/os_demo.wren` demonstrating Platform, Process, and conditional exit usage
- Update `example/regex_demo.wren` to import `Match` and exercise Match object properties, groups, and `matchAll`
- Remove static HTML manual pages (`base64.html`, `dns.html`, `json.html`) and replace with structured `manual_src/` directory containing Jinja2 templates, YAML metadata, and content pages
- Expand `README.md` with build targets table, manual building instructions, source layout, and guide for adding new module documentation
2026-01-26 05:12:14 +01:00
{% endblock %}