Files and folders
Otter has native file operations written as plain statements. They are safe by default: deleting a folder is never recursive and refuses a folder that still has things in it, and write ... atomically replaces a file in one step. They are not a sandbox: a program can read and write any path the user running it can.
Otter
notePath is "status.txt"
# Writing text to a file
write "Otter system: online and verified." to notePath
# Checking file existence
if file notePath exists
say "Note file exists on disk."
.
# Reading file contents
read notePath into content
say "File content:" content
# Copying and deleting files
backupPath is "status-backup.txt"
copy notePath to backupPath
delete file notePath
delete file backupPath
say "Temporary files cleaned up."
Output
Note file exists on disk.
File content: Otter system: online and verified.
Temporary files cleaned up.
Watching files change
A program can react to the file system as it happens. The watcher is real operating-system events, not polling:
Otter
watch file "settings.json" and call it settingsWatcher
on change of settingsWatcher
say "settings changed"
stop watching settingsWatcher
.
watch folder "reports" recursively and call it w also reports on change of, on create in, on delete in, and on rename in events with the path that changed. File watching is for console programs; the web target reports it as unsupported.
Appending, copying and moving
write replaces a file's contents. append adds to the end. copy and move take a source and a destination.
Otter
write "one" to "log.txt"
append "two" to "log.txt"
read "log.txt" into text
say text
copy "log.txt" to "backup.txt"
move "backup.txt" to "archive.txt"
delete file "archive.txt"
delete file "log.txt"
Output
onetwo
append adds exactly what you give it, with no line break of its own. Put a line break in the text if you want one.
Listing files
Otter
get files in "." into found
say "Files here:" length of found
each item in found
say name of item
.
Each entry is an object with properties such as name, path, extension, size and modified, read with of. Add and subfolders to look inside folders below as well.
Where am I?
Otter
get current directory into here
say "Running from:" here
Relative paths are relative to this folder - the folder you ran otter from.
Next: CSV and downloads