Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

File Operations

This section covers keywords for working with files in the bot’s storage system. These keywords enable bots to read, write, copy, move, and manage files stored in the bot’s drive bucket.


Overview

General Bots provides a complete set of file operation keywords:

KeywordPurpose
READLoad content from files
WRITESave content to files
DELETE FILERemove files
COPYCopy files within storage
MOVEMove or rename files
LISTList files in a directory
COMPRESSCreate ZIP archives
EXTRACTExtract archive contents
UPLOADUpload files from URLs or users
DOWNLOADSend files to users
GENERATE PDFCreate PDF documents
MERGE PDFCombine multiple PDFs

Quick Examples

Basic File Operations

' Read a file
content = READ "documents/report.txt"
TALK content

' Write to a file
WRITE "Hello, World!" TO "greeting.txt"

' Append to a file
WRITE "New line\n" TO "log.txt" APPEND

' Delete a file
DELETE FILE "temp/old-file.txt"

' Copy a file
COPY "templates/form.docx" TO "user-forms/form-copy.docx"

' Move/rename a file
MOVE "inbox/message.txt" TO "archive/message.txt"

' List files in a directory
files = LIST "documents/"
FOR EACH file IN files
    TALK file.name + " (" + file.size + " bytes)"
NEXT

Working with CSV Data

' Read CSV as structured data
customers = READ "data/customers.csv" AS TABLE

FOR EACH customer IN customers
    TALK customer.name + ": " + customer.email
NEXT

' Write data as CSV from database query
orders = FIND "orders" WHERE status = "pending" LIMIT 100
WRITE orders TO "exports/orders.csv" AS TABLE

File Upload and Download

' Accept file from user
TALK "Please send me a document."
HEAR user_file
result = UPLOAD user_file TO "uploads/" + user.id
TALK "File saved: " + result.filename

' Send file to user
DOWNLOAD "reports/summary.pdf" AS "Monthly Summary.pdf"
TALK "Here's your report!"

PDF Operations

' Generate PDF from template
GENERATE PDF "templates/invoice.html" TO "invoices/inv-001.pdf" WITH
    customer = "John Doe",
    amount = 150.00,
    date = FORMAT(NOW(), "YYYY-MM-DD")

' Merge multiple PDFs
MERGE PDF ["cover.pdf", "report.pdf", "appendix.pdf"] TO "complete-report.pdf"

Archive Operations

' Create a ZIP archive
COMPRESS ["doc1.pdf", "doc2.pdf", "images/"] TO "package.zip"

' Extract archive contents
EXTRACT "uploaded.zip" TO "extracted/"

Storage Structure

Files are stored in the bot’s drive bucket with the following structure:

bot-name/
├── documents/
├── templates/
├── exports/
├── uploads/
│   └── user-123/
├── reports/
├── temp/
└── archives/

Path Rules

PathDescription
file.txtRoot of bot’s storage
folder/file.txtSubdirectory
folder/sub/file.txtNested subdirectory
../file.txtNot allowed — no parent traversal
/absolute/pathNot allowed — paths are always relative
' Valid paths
content = READ "documents/report.pdf"
WRITE data TO "exports/2025/january.csv"

' Invalid paths (will error)
' READ "../other-bot/file.txt"  ' Parent traversal blocked
' READ "/etc/passwd"            ' Absolute paths blocked

Supported File Types

Text Files

ExtensionDescription
.txtPlain text
.mdMarkdown
.jsonJSON data
.csvComma-separated values
.xmlXML data
.htmlHTML documents
.yamlYAML configuration

Documents

ExtensionDescriptionAuto-Extract
.pdfPDF documents✓ Text extracted
.docxWord documents✓ Text extracted
.xlsxExcel spreadsheets✓ As table data
.pptxPowerPoint✓ Text from slides

Media

ExtensionDescription
.jpg, .png, .gifImages
.mp3, .wavAudio
.mp4, .movVideo

Archives

ExtensionDescription
.zipZIP archives
.tar.gzCompressed tarballs

Common Patterns

Template Processing

' Load template and fill placeholders
template = READ "templates/welcome-email.html"

email_body = REPLACE(template, "{{name}}", customer.name)
email_body = REPLACE(email_body, "{{date}}", FORMAT(NOW(), "MMMM DD, YYYY"))
email_body = REPLACE(email_body, "{{order_id}}", order.id)

SEND MAIL customer.email, "Welcome!", email_body

Data Export

' Export query results to CSV
results = FIND "orders" WHERE status = "completed" AND date > "2025-01-01"
WRITE results TO "exports/completed-orders.csv" AS TABLE

' Generate download link
link = DOWNLOAD "exports/completed-orders.csv" AS LINK
TALK "Download your export: " + link

Backup and Archive

' Create dated backup
backup_name = "backups/data-" + FORMAT(NOW(), "YYYYMMDD") + ".json"
data = GET BOT MEMORY "important_data"
WRITE JSON_STRINGIFY(data) TO backup_name

' Archive old files
old_files = LIST "reports/2024/"
COMPRESS old_files TO "archives/reports-2024.zip"

' Clean up originals
FOR EACH file IN old_files
    DELETE FILE file.path
NEXT

File Validation

' Check file exists before processing
files = LIST "uploads/" + user.id + "/"
document_found = false

FOR EACH file IN files
    IF file.name = expected_filename THEN
        document_found = true
        EXIT FOR
    END IF
NEXT

IF document_found THEN
    content = READ "uploads/" + user.id + "/" + expected_filename
    ' Process content...
ELSE
    TALK "I couldn't find that document. Please upload it again."
END IF

Organize Uploads

' Organize uploaded files by type
HEAR uploaded_file

file_type = uploaded_file.mime_type

IF INSTR(file_type, "image") > 0 THEN
    folder = "images"
ELSE IF INSTR(file_type, "pdf") > 0 THEN
    folder = "documents"
ELSE IF INSTR(file_type, "spreadsheet") > 0 OR INSTR(file_type, "excel") > 0 THEN
    folder = "spreadsheets"
ELSE
    folder = "other"
END IF

result = UPLOAD uploaded_file TO folder + "/" + FORMAT(NOW(), "YYYY/MM")
TALK "File saved to " + folder + "!"

Error Handling

ON ERROR RESUME NEXT

content = READ "documents/important.pdf"

IF ERROR THEN
    PRINT "File error: " + ERROR_MESSAGE
    TALK "Sorry, I couldn't access that file. It may have been moved or deleted."
ELSE
    TALK "File loaded successfully!"
    ' Process content...
END IF

Common Errors

ErrorCauseSolution
FILE_NOT_FOUNDFile doesn’t existCheck path, list directory first
PERMISSION_DENIEDAccess blockedCheck file permissions
PATH_TRAVERSALInvalid path with ..Use only relative paths
FILE_TOO_LARGEExceeds size limitIncrease limit or split file
INVALID_FORMATUnsupported file typeConvert or use different format

Configuration

Configure file operations in config.csv:

name,value
drive-provider,seaweedfs
drive-url,http://localhost:8333
drive-bucket,my-bot
drive-read-timeout,30
drive-write-timeout,60
drive-max-file-size,52428800
drive-allowed-extensions,pdf,docx,xlsx,jpg,png,csv,json

Size Limits

OperationDefault LimitConfigurable
Read file50 MBYes
Write file50 MBYes
Upload file50 MBYes
Total storage10 GB per botYes
Files per directory10,000Yes

Security Considerations

  1. Path validation — All paths are sanitized to prevent directory traversal
  2. File type restrictions — Executable files blocked by default
  3. Size limits — Prevents storage exhaustion attacks
  4. Access control — Files isolated per bot
  5. Malware scanning — Uploaded files scanned before storage

See Also