Skip to the page
Najova

Docs

/

Guides

Files and programs

Najova scripts can read and write files, work through folders, take values from the command line and run other programs: the everyday jobs of a tool.

Reading and writing

$writeFile writes a file in place of what was in it, and $appendFile adds to its end. $readFile gives back its text. Paths start at the script's folder, so notes.txt is beside the script and old/2026 is a folder inside it.

notes.naja
$writeFile[notes.txt;Buy flour\n]
$appendFile[notes.txt;Order rye\n]
$print[$trim[$readFile[notes.txt]]]

$makeFolder[old/2026]
$copyFile[notes.txt;old/2026/notes.txt]
$each[name;$files[old/2026]]
  $print[In old/2026: $get[name]]
$endeach
$print[Is there a cakes.txt? $exists[cakes.txt]]

› naja run notes.naja

Buy flour
Order rye
In old/2026: notes.txt
Is there a cakes.txt? false

$files lists the names in a folder, $exists says whether a file or folder is there, $makeFolder makes one (with the folders above it), and $copyFile copies a file or a whole folder. $deleteFile deletes a file, and doesn't touch folders.

Working through a file

Text from a file is a value like any other. Here $split cuts a spreadsheet's rows into lines and each line into cells, $number turns a cell into a number, and the total goes into a report.

sales.naja
$-- Read a file line by line, add up a column and write a report.
$let[lines;$split[$trim[$readFile[sales.csv]];"\n"]]
$let[total;0]
$each[line;$slice[$get[lines];2]]
  $let[cells;$split[$get[line];,]]
  $set[total;$get[total] + $number[$item[$get[cells];2]]]
$endeach
$let[breads;$count[$get[lines]] - 1]
$writeFile[report.txt;$get[breads] breads, $get[total] loaves sold]
$print[$readFile[report.txt]]

› naja run sales.naja

3 breads, 45 loaves sold

sales.csv is beside the script: a heading line, then sourdough,14 and rye,9 and bagel,22.

For data you change often, dataNaja keeps rows for you and checks each one, so you don't write the file handling yourself.

Values from outside

$args is a list of what was typed after the file's name, and $env reads a value from the computer's environment, like a password or a token, so it never has to be written in the script. $exit ends the script at once: $exit[1] tells whoever ran it that something went wrong.

greet.naja
$-- Values typed after the file's name, and from the environment.
$let[who;$item[$args;1]]
$if[$get[who] == nil]
  $print[Say who to greet: naja run greet.naja Sam]
  $exit[1]
$endif
$let[shop;$env[BAKERY_NAME]]
$if[$get[shop] == nil]
  $set[shop;the bakery]
$endif
$print[Hello $get[who], welcome to $get[shop]!]

› naja run greet.naja Sam

Hello Sam, welcome to the bakery!

Running other programs

$runProgram runs another program, waits for it, and gives back what it printed (out), its mistakes (err) and its code, which is 0 when it went well. Give its values as a list, so a name with spaces in it stays one value.

commit.naja
$let[r;$runProgram[git;$list[log;-1;--format=%s]]]
$if[$get[r.code] == 0]
  $print[Last change: $trim[$get[r.out]]]
$else
  $print[git said: $get[r.err]]
$endif

$startProgram starts a program and goes on without waiting, like a server to test against, and $stopProgram stops it. Anything still running stops when the script ends. $tempFolder gives a new empty folder to work in, and it's deleted at the end too.

Keeping scripts in their folder

naja run tool.naja --safe lets a script use only its own folder and the folders in it, turns off $fetch, and doesn't let it run other programs. Use it for a script you didn't write yourself. Web pages never run other programs while answering a visitor.