# 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.
Parameter Takes to required any value overwrite optional a flag: overwrite or not overwrite
Arguments can be named: ->copy_to(…, overwrite: …).
Example
.kscript XML Copy
<import package="KScript.IO" />
<do>@write_text('a.txt', 'a')</do>
<echo>{ @file('a.txt')->copy_to('b.txt').name }</echo>.kscript XML Copy
import KScript.IO
@write_text('a.txt', 'a')
echo @file('a.txt')->copy_to('b.txt').nameDeletes the file, or the folder and everything in it. True if something was deleted.
Example
.kscript XML Copy
<import package="KScript.IO" />
<do>@write_text('old.txt', 'x')</do>
<echo>{ @file('old.txt')->delete() } { @exists('old.txt') }</echo>.kscript XML Copy
import KScript.IO
@write_text('old.txt', 'x')
echo "{ @file('old.txt')->delete() } { @exists('old.txt') }"A file value for the joined path. Parts can be separate arguments or a list, as in $dir->join(['a', 'b.txt']).
Example
.kscript XML Copy
<import package="KScript.IO" />
<echo>{ @file('reports')->join('2026', 'q3.pdf').name }</echo>.kscript XML Copy
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.
Parameter Takes to required any value overwrite optional a flag: overwrite or not overwrite
Arguments can be named: ->move_to(…, overwrite: …).
Example
.kscript XML Copy
<import package="KScript.IO" />
<do>@write_text('draft.txt', 'a')</do>
<echo>{ @file('draft.txt')->move_to('final.txt').name } { @exists('draft.txt') }</echo>.kscript XML Copy
import KScript.IO
@write_text('draft.txt', 'a')
echo "{ @file('draft.txt')->move_to('final.txt').name } { @exists('draft.txt') }"Every byte of the file as a base64 value, including binary data.
Example
.kscript XML Copy
<import package="KScript.IO" />
<do>@write_text('a.txt', 'hi')</do>
<echo>{ @file('a.txt')->read_bytes() }</echo>.kscript XML Copy
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.
Parameter Takes as optional type
Example
.kscript XML Copy
<import package="KScript.IO" />
<do>@write_json('settings.json', { theme: 'dark' })</do>
<echo>{ @file('settings.json')->read_json().theme }</echo>.kscript XML Copy
import KScript.IO
@write_json('settings.json', { theme: 'dark' })
echo @file('settings.json')->read_json().themeThe path relative to a folder.
Example
.kscript XML Copy
<import package="KScript.IO" />
<echo>{ @file('reports/2026/q3.pdf')->relative_to('reports') }</echo>.kscript XML Copy
import KScript.IO
echo @file('reports/2026/q3.pdf')->relative_to('reports')The SHA-256 hash of the file's bytes, in hex.
Example
.kscript XML Copy
<import package="KScript.IO" />
<do>@write_text('a.txt', 'hello')</do>
<echo>{ @file('a.txt')->sha256() }</echo>.kscript XML Copy
import KScript.IO
@write_text('a.txt', 'hello')
echo @file('a.txt')->sha256()Writes text to the file, replacing it. Folders are created as needed. Returns the file.
Example
.kscript XML Copy
<import package="KScript.IO" />
<echo>{ @file('notes/today.txt')->write_text('Buy milk')->read_text() }</echo>.kscript XML Copy
import KScript.IO
echo @file('notes/today.txt')->write_text('Buy milk')->read_text()