What's New in Director 8.5 > Multiuser Server-Side Scripting > Accessing files on the server |  |

Accessing files on the server

In order to create script objects from the text files in the scripts folder, the server makes use of file-access Lingo that is included in the LingoVM Xtra. These Lingo elements allow you to read from and work with files on the server computer. You can work with text files as well as files of other types that can contain any value that can be stored in a Lingo variable. You can read and write integers, floating-point numbers, lists, images, and more.

You perform these operations by using the file-access Lingo elements. These commands and functions allow you to get information about the size, content, and locked state of files, as well as add new files, edit the content of existing files, and delete files.

To check whether a file you want to read is present on the server computer, use the `exists()` function. The following handler checks whether the file Sunset.jpg exists in the test_files folder on the server computer.

    on checkForFile
    	return file("C:\test_files\Sunset.jpg").exists
    end

Once you know a file exists, you can read the contents of the file into a variable with the `read()` function. The following statement reads the contents of the file Sunset.jpg and assigns them to the variable `myImage`:

    myImage = file("C:\test_files\Sunset.jpg").read()

If you want to read only a part of the file, you can specify the number of bytes to be read. The following statement reads just the first 400 bytes (characters) from the file LongSpeech.txt and assigns them to the variable `theText`.

    theText = file("C:\test_files\LongSpeech.txt").read(400)

To read from files other than text files, use the `readValue()` function.

To change the contents of a text file or create a new text file, use the `write()` function. To write any type of Lingo value to a file, use the `writeValue()` function.

These statements write the string "Four score and seven years ago..." to the file Gettysburg.txt:

    theString = "Four score and seven years ago..."
    file("C:\test_files\Gettysburg.txt").write(theString)

This statement writes the contents of the variable `tempImage`, which contains bitmap image data, to the file Newimage.tmp:

    file("C:\test_files\NewImage.tmp").writeValue(tempImage)

Using `write()` and `writeValue()` will cause the entire file to be overwritten. It is best to read the entire file into a variable, manipulate the variable, and then rewrite the entire file.

You can also copy files with the `copyTo()` command. The following statement copies the contents of the file Gettysburg.txt into the file Longspeech.txt:

    file("C:\test_files\Gettysburg.txt").copyTo \
    ("C:\test_files\Longspeech.txt")

For more information on other kinds of file manipulation with Lingo, see Multiuser Lingo Dictionary overview.