KScript docs
EN

Types and validation

File paths

A file_path says where a file or folder is. Write it without quotes, starting from where the script is:

  • ./data/input.txt is in the data folder next to the script, and ./ on its own is the script's folder.
  • ../shared/rates.json goes up one folder first. Repeat it to go up further: ../../rates.json.
  • ~/notes.md starts from your home folder.

Every command that reads or writes files takes one, so a script that reads its puzzle input or its settings needs no quotes and no joining:

<import package="KScript.IO" />

<let name="lines" value="@read_lines(./data/input.txt)" />
<echo value="$lines->map(.to_number())->sum()" />

<let name="day" value="3" />
<let name="notes" value="@read_text(./days/day{ $day }.txt)" />
<let name="settings" value="@read_json(../shared/settings.json)" />
<do>@write_text(./out/report.txt, $notes)</do>
import KScript.IO

$lines = @read_lines(./data/input.txt)
echo $lines->map(.to_number())->sum()

$day = 3
$notes = @read_text(./days/day{ $day }.txt)
$settings = @read_json(../shared/settings.json)
@write_text(./out/report.txt, $notes)

As you type a path, the editor lists the files and folders that are there: after ./ it shows what is next to the script, folders first, and picking a folder such as ./data/ shows what is inside it. Names that start with a dot, such as .env, show once you type the dot.

Quote a path that is absolute or has spaces in it: @read_text('/var/log/app.log'), @file('C:\Reports\May 2026.pdf'). Quoted text and text in a variable work wherever a file_path is expected, and relative text such as 'data/input.txt' is read from the script's folder too.

A file path is kept as written, so it prints the way you wrote it. Its fields read its parts without touching the disk:

<let name="report" value="./reports/may/summary.final.pdf" />
<echo value="$report.name" /> <!-- summary.final.pdf -->
<echo value="$report.stem" /> <!-- summary.final -->
<echo value="$report.extension" /> <!-- pdf -->
<echo value="$report.folder" /> <!-- ./reports/may -->
<echo value="./reports + 'may/summary.pdf'" />
$report = ./reports/may/summary.final.pdf
echo $report.name       // summary.final.pdf
echo $report.stem       // summary.final
echo $report.extension  // pdf
echo $report.folder     // ./reports/may
echo ./reports + 'may/summary.pdf'
Field or operator What it gives
.name The last name, extension included: summary.final.pdf.
.stem The last name without its extension: summary.final.
.extension What follows the last dot, without the dot: pdf, or '' when there is none.
.folder The folder it is in, as a file_path: ./reports/may. It is null for ./ itself.
.segments The names as written, starting with ., .. or ~: ['.', 'reports', 'may', 'summary.final.pdf'].
+ 'name' The path with more names added after a /: ./reports + 'may' is ./reports/may.

To find out about the file itself (its size, when it changed, whether it is there), use @file(./reports/may/summary.pdf), @exists(...) and @is_folder(...).