External Files

From Director Online Wiki
Jump to: navigation, search

Text Files

There are many ways/xtras to read/write/etc text files with lingo. One is of course the free onboard xtra FileIo. But there are others to mention, such as the FileXtra (free), of course BuddyAPI (commercial, 2 functions free), ...

read

FileIO

on fileRead( aFilePath )
  --returns:        string fileContents
  --params:         string filePath
  --description:    reads a complete .txt file
  --                no idea about mac line breaks etc in this version.
  --xtras:          fileIO
  --example:        put fileRead( "c:\test.txt" )
  --                -- "line 1
  --                line 2"
  
  vFIO = new(xtra("fileIo"))
  vFIO.openFile(aFilePath, 1) --1=read
  
  vStr = vFIO.readFile()
  
  vFIO.closeFile()
  vFIO = VOID
  
  return vStr
end fileRead

write

FileIO

on fileWrite( aStr, aFilePath )
  --returns:        err code
  --params:         string, string filePath
  --description:    writes (and overwrites) a complete .txt file.
  --                no idea about mac line breaks etc in this version
  --xtras:          fileIO
  --example:        put fileWrite( "line 1"&RETURN&"line 2", "c:\test.txt" ) 
  --                -- 0
  
  -- create the FileIO instance
  vFIO = new(xtra("fileIo"))
  
  -- delete existing file, if ness.
  vFIO.openFile(aFilePath,2)
  vFIO.delete()
  
  -- create and open the file
  vFIO.createFile(aFilePath)
  vFIO.openFile(aFilePath,2)
  
  -- check to see if file opened ok
  if vFIO.status()<>0 then
    err = vFIO.error(vFIO.status())
    return err
  end if
  
  -- write the file
  vFIO.writeString( aStr )
  
  -- close the file
  vFIO.closeFile()
  
  vFIO = void
  
  return 0
end fileWrite

append

FileIO

on fileAppend( aStr, aFilePath )
  --returns:        err code
  --params:         string, string filePath
  --description:    writes a new line after a text file. creates file if ness.
  --xtras:          fileIO
  --example:        put fileAppend( "this is a new new line", "c:\test.txt" )
  --                -- 0
  
  -- leading linebreak
  aStr = RETURN&numToChar(10) & aStr -- how about the mac line break here? anyone? -- ben
  
  -- create the FileIO instance
  vFIO = new(xtra("fileIo"))
  
  -- open file
  vFIO.openFile( aFilePath,2 )
  
  
  -- check to see if file opened ok (exists file)
  vStatus = vFIO.status()
  case vStatus of
      
    -37, -43: 
      -- -37="Bad file name"
      -- -43="File not found"
      
      -- create and open the file
      vFIO.createFile(aFilePath)
      vFIO.openFile(aFilePath,2)
      aStr = aStr.char[ 3..aStr.length ]
  end case   
  
  
  -- check to see if file opened ok
  if vFIO.status()<>0 then
    err = vFIO.error( vFIO.status() )
    return err -- ERR
  end if
  
  
  -- Set position to end of file
  vFIO.setPosition( vFIO.getLength() ) 
  
  -- write the file
  vFIO.writeString( aStr )
  
  -- close the file
  vFIO.closeFile()
  
  vFIO = void
  
  return 0
end fileAppend

delete

FileIO

on fileDelete( aFilePath )
  --returns:        0 or err code
  --params:         string filePath
  --description:    deletes a file
  --xtras:          fileIO
  --example:        put fileDelete( "c:\test.txt" )
  --                -- 0
  
  vFIO = new(xtra "fileIO")  
  vFIO.openFile(aFilePath,0)
  vFIO.delete()
  vFIO = void  
  return 0
end fileDelete


Registered Files

read

Here you can see various ways to open files with their associated application.

BuddyAPI

baOpenFile( "c:\test.doc", "maximized" )

FileXtra4

xtra("FileXtra4").new().fx_FileOpenDocument( "c:\test.doc" )

Databases

check here for details