Files

functions for reading, writing, and manipulating files


Functions

Predicates


The Files module provides powerful functions for file operations, with intelligent handling of different data sources and formats. Its flexibility allows seamless interaction with local files, URLs, and direct content.

Key Concepts

  • Smart source detection (URLs, files, direct content)
  • Support for multiple file formats (JSON, CSV, XML, etc.)
  • File system operations (copy, move, delete)
  • Directory handling and path manipulation
  • Automatic format detection and parsing

Basic Usage

Reading Content

read intelligently handles different types of sources:

; reading from different sources
content: read "hello.txt"               ; read local file
content: read "https://api.com/data"    ; download from URL
content: read "some direct content"     ; use string directly

; force treating string as file path
content: read.file "some text.txt"      ; error if file doesn't exist

Writing Content

; basic writing
write "some content" "output.txt"

; using the infix operator
"some content" >> "output.txt"

; append to existing file
write.append "more content" "output.txt"

Format Parsing & Generation

; reading & parsing JSON from any source
data: read.json "data.json"              ; from local file
data: read.json "https://api.com/data"   ; from URL
data: read.json "{\"key\":\"value\"}"    ; from string

; writing data as JSON
write.json data "output.json"            ; to file
jsonStr: write.json data ø               ; to string

; other supported formats
content: read.csv "data.csv"             ; parse CSV
content: read.xml "data.xml"             ; parse XML
content: read.html "page.html"           ; parse HTML
content: read.markdown "doc.md"          ; convert to HTML
content: read.toml "config.toml"         ; parse TOML

Tip
The format functions (like .json, .csv) work the same way regardless of source - local file, URL, or direct content.

File System Operations

; check existence
print exists? "file.txt"      ; => true/false
print file? "file.txt"        ; => true/false
print directory? "folder"     ; => true/false

; file operations
copy "source.txt" "dest.txt"
move "old.txt" "new.txt"
delete "unwanted.txt"

; directory operations
copy.directory "src" "dest"
delete.directory "olddir"

Common Patterns

Working with Paths

path: "photos/vacation/beach.jpg"

print extract.directory path    ; photos/vacation
print extract.filename path     ; beach.jpg
print extract.extension path    ; jpg

; normalize paths
print normalize "./folder/../file.txt"  ; file.txt
print normalize "~/documents"           ; expands home directory

Reading Files Safely

; handle potential missing files
if exists? "config.json" [
    settings: read.json "config.json"
]

; force file reading (error if not found)
if throws? [
    data: read.file "important.txt"
]-> print "File not found!"

Bulk Operations

; list files in directory
files: list "documents"
files: list.recursive "documents"    ; include subdirectories

; process multiple files
loop files 'file [
    if "txt" = extract.extension file ->
        print ["Found text file:" file]
]