storage fields and methods A field is a plain fact about the value, read with a dot and no brackets: $value.name. A method takes arguments or does work, and is called with ->: $value->name(arguments), or with the value piped in: $value |> name(arguments).
Fields Field Type Description .appstring The app the folder belongs to. .kindstring Which of the app's folders it is: config, data, cache, state or logs. .pathstring The folder's full path.
From KScript.Storage These need <import package="KScript.Storage" />import KScript.Storage . See KScript.Storage .
Adds text to the end of a file.
Arguments can be named: ->append(…, text: …).
Example
.kscript XML Copy
<do>@storage(logs)->append('app.log', 'Started' + @char_of(10))</do>.kscript XML Copy
@storage(logs)->append('app.log', 'Started' + @char_of(10))Deletes everything in the folder. Returns how many files were deleted.
Example
.kscript XML Copy
<echo>{ @storage(cache)->clear() } files removed</echo>.kscript XML Copy
echo "{ @storage(cache)->clear() } files removed"# storage ->copy (from , to , overwrite?: bool ) KScript.Storage Copies a file or folder inside the folder. overwrite replaces what is at to.
Parameter Takes from required any value to required any value overwrite optional a flag: overwrite or not overwrite
Arguments can be named: ->copy(…, to: …, overwrite: …).
Example
.kscript XML Copy
<do>@storage(data)->write('a.txt', 'hi'); @storage(data)->copy('a.txt', 'b.txt', overwrite)</do>
<echo>{ @storage(data)->read('b.txt') }</echo>.kscript XML Copy
@storage(data)->write('a.txt', 'hi')
@storage(data)->copy('a.txt', 'b.txt', overwrite)
echo @storage(data)->read('b.txt')Deletes a file, or a folder and everything in it. True if something was deleted.
Example
.kscript XML Copy
<do>@storage(cache)->delete('thumbnails')</do>.kscript XML Copy
@storage(cache)->delete('thumbnails')True if the file (or, without a name, the folder) is there.
Example
.kscript XML Copy
<echo>{ @storage(config)->exists('settings.json') }</echo>.kscript XML Copy
echo @storage(config)->exists('settings.json')A file in the folder as a file value, with .size, .modified, .mime and the file methods.
Example
.kscript XML Copy
<do>@storage(data)->write('notes.txt', 'Buy milk')</do>
<echo>{ @storage(data)->file('notes.txt').size } bytes</echo>.kscript XML Copy
@storage(data)->write('notes.txt', 'Buy milk')
echo "{ @storage(data)->file('notes.txt').size } bytes"A folder inside this one, with the same methods.
Example
.kscript XML Copy
<let name="notes" value="@storage(data)->folder('notes')" />
<do>$notes->write('today.txt', 'Buy milk')</do>.kscript XML Copy
$notes = @storage(data)->folder('notes')
$notes->write('today.txt', 'Buy milk')# storage ->list (pattern? , recursive?: bool ) → list KScript.Storage The names of the files in the folder, such as ['notes/a.txt', 'settings.json']. pattern is a wildcard like '.json', or a regex like /^notes/. .md$/ matched against the whole name.
Parameter Takes pattern optional any value recursive optional a flag: recursive or not recursive
Arguments can be named: ->list(…, recursive: …).
Example
.kscript XML Copy
<for each="name" in="@storage(data)->list('*.txt', recursive)"><echo>{ $name }</echo></for>.kscript XML Copy
for name in @storage(data)->list('*.txt', recursive) {
echo $name
}# storage ->move (from , to , overwrite?: bool ) KScript.Storage Moves (or renames) a file or folder inside the folder. overwrite replaces what is at to.
Parameter Takes from required any value to required any value overwrite optional a flag: overwrite or not overwrite
Arguments can be named: ->move(…, to: …, overwrite: …).
Example
.kscript XML Copy
<do>@storage(data)->write('draft.txt', 'hi'); @storage(data)->move('draft.txt', 'final.txt')</do>
<echo>{ @storage(data)->exists('final.txt') }</echo>.kscript XML Copy
@storage(data)->write('draft.txt', 'hi')
@storage(data)->move('draft.txt', 'final.txt')
echo @storage(data)->exists('final.txt')The full path of a file in the folder. The folder's own path is the field .path.
Example
.kscript XML Copy
<echo>{ @storage(data)->path_of('notes/today.txt') }</echo>.kscript XML Copy
echo @storage(data)->path_of('notes/today.txt')A file's text, or default (null) when it isn't there yet.
Arguments can be named: ->read(…, default: …).
Example
.kscript XML Copy
<echo>{ @storage(data)->read('notes.txt', default: '') }</echo>.kscript XML Copy
echo @storage(data)->read('notes.txt', default: '')Every byte of a file as a base64 value, or default (null) when it isn't there yet.
Arguments can be named: ->read_bytes(…, default: …).
Example
.kscript XML Copy
<do>@storage(data)->write('logo.txt', 'hi')</do>
<echo>{ @storage(data)->read_bytes('logo.txt') }</echo>.kscript XML Copy
@storage(data)->write('logo.txt', 'hi')
echo @storage(data)->read_bytes('logo.txt')# storage ->read_json (name , default? , as?: type ) → any KScript.Storage Reads JSON and checks it against the optional as type, mapping a list item by item. A missing file returns default (null if omitted) as supplied.
Parameter Takes name required any value default optional any value as optional type
Arguments can be named: ->read_json(…, default: …, as: …).
Example
.kscript XML Copy
<type name="Settings" fields="theme: string" />
<let name="settings" value="@storage(config)->read_json('settings.json', as: Settings)" />
<echo>{ $settings?.theme ?? 'light' }</echo>.kscript XML Copy
type Settings {
theme: string
}
$settings = @storage(config)->read_json('settings.json', as: Settings)
echo $settings?.theme ?? 'light'Writes text to a file, replacing it safely. Folders are created as needed.
Arguments can be named: ->write(…, text: …).
Example
.kscript XML Copy
<do>@storage(data)->write('notes/today.txt', 'Buy milk')</do>.kscript XML Copy
@storage(data)->write('notes/today.txt', 'Buy milk')# storage ->write_bytes (name , bytes: base64 ) KScript.Storage Writes the exact bytes of a base64 value (an upload's .bytes, say) to a file, replacing it safely.
Parameter Takes name required any value bytes required base64
Arguments can be named: ->write_bytes(…, bytes: …).
Example
.kscript XML Copy
<do>@storage(data)->write_bytes('avatars/1.bin', 'hi'->to_base64())</do>.kscript XML Copy
@storage(data)->write_bytes('avatars/1.bin', 'hi'->to_base64())# storage ->write_json (name , value ) KScript.Storage Writes a value to a file as indented JSON, replacing it safely.
Arguments can be named: ->write_json(…, value: …).
Example
.kscript XML Copy
<do>@storage(config)->write_json('settings.json', { theme: 'dark', size: 14 })</do>.kscript XML Copy
@storage(config)->write_json('settings.json', { theme: 'dark', size: 14 })Methods from any