File.writeLines

From MiniScript Wiki
Revision as of 22:14, 15 August 2021 by JoeStrout (talk | contribs)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search

In Mini Micro and command-line MiniScript, the file.writeLines function writes a single value, or an array of values, to a file.


file.writeLines will create or overwrite the file path passed to it. This function returns null on success, or an error string in the event of an error.

Arguments

Parameter Name Meaning
filePath an absolute or relative path of the file or directory to be written to.
content a single variable (of any type) to be written to the file. If it is an array, it will be written out with a newline after each item in the array.

See also: file.readLines

Example 1

foods = [
  "fish",
  "grapes",
  "dreams",
  "head cheese",
  "cucumber",
  "sea cucumber",
  "Welsh rabbit",
]

file.writeLines "myFavoriteFoods.txt", foods

file.writeLines "myAge.txt", 100

file.writeLines "../myParentsAge.txt", 242

Example 2

This silly program will overwrite itself each time it is ran, incrementing the count. Name it selfcounter.ms and give it a whirl!

count = 0

// Increment count and report.
count = count + 1
print "This file has been ran: " + count + " time(s)!"

// Read source code of this file, modify it, and write it back out. 	
code = file.readLines("selfcounter.ms")

code[0] = "count = " + count

file.writeLines "selfcounter.ms", code

// Reload entire file. 
load "selfcounter.ms"