KScript docs
EN

file fields and methods

File values, from @file(), @files() and @folders().

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

FieldTypeDescription
.createddateWhen the file was created.
.directoryfileThe folder the file is in.
.existsboolTrue when a file or folder is there.
.extensionstringThe extension without the dot: pdf (empty if there isn't one).
.is_fileboolTrue when it is a file.
.is_folderboolTrue when it is a folder.
.mimemimeThe media type its extension names, such as image/png; application/octet-stream when the extension isn't a known one.
.modifieddateWhen the file was last changed.
.namestringThe file name with its extension: report.pdf.
.pathstringThe full path as text.
.sizenumberThe size in bytes (0 for folders).
.stemstringThe file name without its extension: report.

From KScript.IO

These need <import package="KScript.IO" />import KScript.IO. See KScript.IO.

#file->copy_to(to, overwrite?: bool) → file KScript.IO

Copies the file or folder and returns the copy. to is a full path, or an existing folder to copy it into.
ParameterTakes
to requiredany value
overwrite optionala flag: overwrite or not overwrite

Arguments can be named: ->copy_to(…, overwrite: …).

Example
<import package="KScript.IO" />
<do>@write_text('a.txt', 'a')</do>
<echo>{ @file('a.txt')->copy_to('b.txt').name }</echo>
import KScript.IO
@write_text('a.txt', 'a')
echo @file('a.txt')->copy_to('b.txt').name

#file->delete() → bool KScript.IO

Deletes the file, or the folder and everything in it. True if something was deleted.
Example
<import package="KScript.IO" />
<do>@write_text('old.txt', 'x')</do>
<echo>{ @file('old.txt')->delete() } { @exists('old.txt') }</echo>
import KScript.IO
@write_text('old.txt', 'x')
echo "{ @file('old.txt')->delete() } { @exists('old.txt') }"

#file->join(parts...) → file KScript.IO

A file value for the joined path. Parts can be separate arguments or a list, as in $dir->join(['a', 'b.txt']).
Example
<import package="KScript.IO" />
<echo>{ @file('reports')->join('2026', 'q3.pdf').name }</echo>
import KScript.IO
echo @file('reports')->join('2026', 'q3.pdf').name

#file->move_to(to, overwrite?: bool) → file KScript.IO

Moves (or renames) the file or folder and returns it at its new place. to is a full path, or an existing folder to move it into.
ParameterTakes
to requiredany value
overwrite optionala flag: overwrite or not overwrite

Arguments can be named: ->move_to(…, overwrite: …).

Example
<import package="KScript.IO" />
<do>@write_text('draft.txt', 'a')</do>
<echo>{ @file('draft.txt')->move_to('final.txt').name } { @exists('draft.txt') }</echo>
import KScript.IO
@write_text('draft.txt', 'a')
echo "{ @file('draft.txt')->move_to('final.txt').name } { @exists('draft.txt') }"

#file->read_bytes() → base64 KScript.IO

Every byte of the file as a base64 value, including binary data.
Example
<import package="KScript.IO" />
<do>@write_text('a.txt', 'hi')</do>
<echo>{ @file('a.txt')->read_bytes() }</echo>
import KScript.IO
@write_text('a.txt', 'hi')
echo @file('a.txt')->read_bytes()

#file->read_json(as?: type) → any KScript.IO

The file read as JSON. With as: a type, the data is checked and turned into records.
ParameterTakes
as optionaltype
Example
<import package="KScript.IO" />
<do>@write_json('settings.json', { theme: 'dark' })</do>
<echo>{ @file('settings.json')->read_json().theme }</echo>
import KScript.IO
@write_json('settings.json', { theme: 'dark' })
echo @file('settings.json')->read_json().theme

#file->read_lines() → list KScript.IO

The file's lines.
Example
<import package="KScript.IO" />
<do>@write_text('a.txt', 'one' + @char_of(10) + 'two')</do>
<echo>{ @file('a.txt')->read_lines() }</echo>
import KScript.IO
@write_text('a.txt', 'one' + @char_of(10) + 'two')
echo @file('a.txt')->read_lines()

#file->read_text() → string KScript.IO

The file's text.
Example
<import package="KScript.IO" />
<do>@write_text('a.txt', 'hello')</do>
<echo>{ @file('a.txt')->read_text() }</echo>
import KScript.IO
@write_text('a.txt', 'hello')
echo @file('a.txt')->read_text()

#file->relative_to(folder) → string KScript.IO

The path relative to a folder.
Example
<import package="KScript.IO" />
<echo>{ @file('reports/2026/q3.pdf')->relative_to('reports') }</echo>
import KScript.IO
echo @file('reports/2026/q3.pdf')->relative_to('reports')

#file->sha256() → string KScript.IO

The SHA-256 hash of the file's bytes, in hex.
Example
<import package="KScript.IO" />
<do>@write_text('a.txt', 'hello')</do>
<echo>{ @file('a.txt')->sha256() }</echo>
import KScript.IO
@write_text('a.txt', 'hello')
echo @file('a.txt')->sha256()

#file->write_text(text) → file KScript.IO

Writes text to the file, replacing it. Folders are created as needed. Returns the file.
Example
<import package="KScript.IO" />
<echo>{ @file('notes/today.txt')->write_text('Buy milk')->read_text() }</echo>
import KScript.IO
echo @file('notes/today.txt')->write_text('Buy milk')->read_text()

Methods from any