LessonsAdvanced: Bigger programsLesson 41

Reading and writing files

Read a whole file into a variable and write one back in a line, find the folders a program may use, and list, rename and delete files.

  • Advanced
  • Lesson 41 of 83
  • About 10 minutes
  • 7 examples

A file is a container

The simplest way to use a file in xTalk is to treat it as a container, like a variable or a field. URL "file:" followed by the file's path reads the whole file, and putting into it writes the whole file:

ExampleRun it in the playground
put specialFolderPath("temporary") & "/oxt-lesson-notes.txt" into tPath
put "First line" & return & "Second line" into URL ("file:" & tPath)
put URL ("file:" & tPath) into tText
put the number of lines of tText
put line 2 of tText
put there is a file tPath
delete file tPath
put there is a file tPath
Output
2
Second line
true
false

Use after to add to the end of a file without reading it first, which is handy for a log:

ExampleRun it in the playground
put specialFolderPath("temporary") & "/oxt-lesson-log.txt" into tPath
put "started" into URL ("file:" & tPath)
put return & "working" after URL ("file:" & tPath)
put return & "finished" after URL ("file:" & tPath)
put URL ("file:" & tPath)
delete file tPath
Output
started
working
finished

file: or binfile:

file: reads and writes text, and changes line endings to suit the system. binfile: reads and writes the bytes exactly as they are: use it for pictures, sound and any data that is not plain text, and for text whose encoding you choose yourself:

ExampleRun it in the playground
put specialFolderPath("temporary") & "/oxt-lesson-utf8.txt" into tPath
put textEncode("café ☕", "UTF-8") into URL ("binfile:" & tPath)
put the number of bytes of URL ("binfile:" & tPath)
put textDecode(URL ("binfile:" & tPath), "UTF-8")
delete file tPath
Output
9
café ☕

Where to keep files

A program should not scatter files anywhere. specialFolderPath gives the right folder on every system:

  • specialFolderPath("documents"): the user's documents, for files they made and will look for;
  • specialFolderPath("support"): the place for a program's own settings and data (add a folder with your program's name);
  • specialFolderPath("temporary"): for files you need only for a moment, as in these examples;
  • specialFolderPath("home") and specialFolderPath("desktop").

Paths in xTalk always use / between folders, on Windows too, so the same script works everywhere. With the itemDelimiter set to /, a path's items are its folders:

ExampleRun it in the playground
put "/home/ada/notes/todo.txt" into tPath
set the itemDelimiter to slash
put item -1 of tPath
put item 1 to -2 of tPath
set the itemDelimiter to "."
put item -1 of tPath
Output
todo.txt
/home/ada/notes
txt

Folders and listing files

files(folder) lists the files in a folder, a line each, and folders(folder) its folders. create folder, rename file and delete file and delete folder do what they say:

ExampleRun it in the playground
put specialFolderPath("temporary") & "/oxt-lesson-folder" into tFolder
create folder tFolder
put "a" into URL ("file:" & tFolder & "/b.txt")
put "b" into URL ("file:" & tFolder & "/a.txt")
put "c" into URL ("file:" & tFolder & "/c.csv")
put files(tFolder) into tFiles
sort tFiles
put tFiles
filter tFiles with "*.txt"
put the number of lines of tFiles
rename file (tFolder & "/c.csv") to (tFolder & "/d.csv")
put there is a file (tFolder & "/d.csv")
repeat for each line tFile in files(tFolder)
   delete file (tFolder & "/" & tFile)
end repeat
delete folder tFolder
put there is a folder tFolder
Output
a.txt
b.txt
c.csv
2
true
false

When a file cannot be read

Reading a file that is not there does not stop the script. The URL is empty and the result says what went wrong, so check it:

ExampleRun it in the playground
put URL "file:/no/such/folder/settings.txt" into tSettings
if the result is not empty then
   put "Could not read the settings:" && the result
end if
Output
Could not read the settings: can't open file

Try it yourself

Save a high-score table (one line per player, name,score) to a file in the temporary folder, read it back, add a new score, sort the table highest first, save it again, and put the top line. Delete the file at the end.

Show a solution
ExampleRun it in the playground
put specialFolderPath("temporary") & "/oxt-lesson-scores.txt" into tPath
put "Kim,340" & return & "Lee,875" into URL ("file:" & tPath)
put URL ("file:" & tPath) into tScores
put return & "Raj,1200" after tScores
sort lines of tScores numeric descending by item 2 of each
put tScores into URL ("file:" & tPath)
put line 1 of URL ("file:" & tPath)
delete file tPath
Output
Raj,1200

Words in this lesson

Each one finds every lesson that uses it. The Dictionary has the whole story of each, with its syntax and more examples.