Accessibility

TechNote (Archived)

FileIO Xtra code examples

This TechNote provides examples of the most common uses of FileIO operations.

This FileIO Xtra documentation, ReadMe for the Director FileIO Xtra (TechNote 3177), is useful for Director 5.0.1 and higher. However, only use the version of the FileIO Xtra software that is installed by and with your version of Director. For example, don't use the FileIO Xtra that is installed by Director 6 with Director 8.

For more information about FileIO, see the following TechNotes:

When distributing projectors that use FileIO, make sure to include the Xtra in the projector or in the "Xtras" folder next to the projector. For more information about Xtras and how to properly package them, see Using Xtras in Director: An overview (TechNote 14888).

Reading a text file into a field using the "Open" dialog

The following handler reads a text file into a field cast member named "myfield". The displayOpen method allows the end user to select a file from a standard Macintosh or Windows "Open" dialog box:

Note: In order for this example to work, a field cast member named "myfield" must already exist in the cast.

 on readFromFile     global myFile     if objectP(myFile) then set myFile = 0  --Deletes the instance if it already exists     set myFile = new(xtra "fileio")  -- Creates an instance of FileIO     if the machinetype = 256 then         setfiltermask (myfile, "All files,*.*,Text files,*.txt") -- Sets the filter mask (Win)     else         setfiltermask (myfile, "TEXT")  -- Set the filter mask (Mac)     end if     set fileName = displayOpen(myFile)  -- Displays the "Open" dialog          if not voidP(filename) and not (filename = EMPTY) then              openFile(myFile, filename, 1) -- Opens file that user selected               if status(myFile) = 0 then                     set theFile = readFile(myFile) -- Reads the file into a Lingo variable                                 put theFile into field "myField" -- Displays the text in a field                 else                     alert error(myfile,status(myfile)) -- Displays error message                 end if  end if     closeFile(myFile)  -- Closes the file     set myFile = 0   -- Disposes of the instance end 
Writing data from a field to a text file

The following handler creates and writes a text file. A file named "textfile.txt" is created in the same folder that contains the Director movie. This handler uses the "setFinderInfo" method which makes the text file recognizable by SimpleText on the Macintosh. In Windows, the setFinderInfo method is ignored. Therefore, machine specific Lingo is not necessary.

Note: In order for this example to work, a field cast member named "myfield" must already exist in the cast.

 On writeToFile     global myFile     if objectP(myFile) then set myFile = 0 --Deletes the instance if it already exists     set theFile = the text of field "myfield"  --  Puts some text into a variable     set myFile = new(xtra "fileio")  --  Creates an instance of FileIO     if the moviePath = "" then            alert "No moviePath!  Save your movie and try again."   --  Makes sure moviePath is NOT empty     else         createFile( myfile, the moviepath&"textfile.txt" )  --  Creates the file         openFile( myFile, the moviepath&"textfile.txt",0)  --  Opens the file with R/W access         setfinderinfo(myFile, "TEXT ttxt")  -- Makes readable by SimpleText (Mac only)         writeString( myFile, theFile)  --  Writes text to the file         alert "Status: "&error(myFile,status(myFile))   -- Displays error message       end if     closeFile (myFile)  --  Closes the file     set myFile = 0  --  Disposes of the instance end 
Appending to a text file

The following handler appends data to the end of a text file named "textfile.txt".

Note: In order for this example to work, a field cast member named "myfield" must already exist in the cast, and a file named "textfile.txt" must already exist in the same folder as the Director movie.

 On appendToFile     global myFile     if objectP(myFile) then set myFile = 0  -- Deletes the instance if it already exists     set theFile = the text of field "myfield" -- Puts some text into a variable     set myFile = new(xtra "fileio")   -- Creates an instance of FileIO     if the moviePath = "" then     alert "No moviePath!  Please save your movie and try again."     else         openFile(myfile, the moviepath&"textfile.txt",0)  --Opens the file with R/W access         setPosition(myfile,getLength(myFile))  -- Sets position to end of file         writeString(myFile, theFile)  -- Appends text to the file         alert "Status: "&error(myFile,status(myFile))  -- Displays error message     end if     closeFile (myfile) -- Closes the file     set myFile = 0  -- Disposes of the instance end 
Deleting an existing file

The following handler deletes an existing file named "textfile.txt".

Note: In order for this example to work, a file named "textfile.txt" must already exist in the same folder as the Director movie.

 On deleteFile     global myFile     if objectP(myFile) then set myFile = 0  -- Deletes the instance if it already exists     set myFile = new(xtra "fileio") -- Creates and instance of FileIO     if the moviePath = "" then          alert "No moviePath.  Please save your movie and try again."     Else     openFile (myFile, the moviepath&"textfile.txt",0) -- Opens the file     delete (myFile)     -- Deletes the file     alert "Status:"& error(myFile,status(myFile)) -- Displays error message     end if      closefile (myfile) --  Closes the file     set myFile = 0 -- Disposes of the instance end 
Shortening an existing file

The FileIO Xtra (version 1.0) can not shorten an existing file. To shorten a file, first delete the file, recreate it, and then write the abbreviated data to a new file.

Finding the Windows directory or the Macintosh System folder

GetOSDirectory( ) returns the absolute path to the Windows folder or the Macintosh System folder. Because this method is global, it isn't necessary to create an instance of the Xtra before you call the method, providing that the Xtra is in the Xtras folder. The following Lingo displays the location of the Windows directory in the Message window:

put getOSDirectory( )
-- "C:\MyWin95\"

On the Macintosh, the path would be like this:

put getOsDirectory( )
-- "My HardDrive: System Folder"

AlertThis content requires Flash

To view this content, JavaScript must be enabled, and you need the latest version of the Adobe Flash Player.

Download the free Flash Player now!

Get Adobe Flash Player

Creative Commons License

Search Support


Document Details

ID:tn_3192

Products Affected:

director