WebSiphon 2 Guide: File Functions
contents prev next

addResource
appendFile
copyFile
copyFolder
countResources
fileExists
folderExists
getCreationDate
getFileComments
getFileCreator
getFileSize
getFileType
getFolderContents
getModDate
getResource
getResourceID
getStringResource
isAlias
isFolder
makeAlias
makeFile
makeFolder
moveFile
moveFolder
pathToFileName
readBinaryFile
readFile
removeFile
removeFolder
removeResource
resolveAlias
returnBinaryFile
setFileComments
setFileCreator
setFileName
setFileType
setResource
writeBinaryFile
writeFile
addResource ( file-path, resource-type, resource-id, binary-data [, resource-name ])
Returns: null Location: FileLib
ParameterDescription
file-pathfull path to file
resource-typethe resource type
resource-idthe resource ID
binary-databinary value of resource
[resource-name]the resource name [optional]

Given the full path to a resource file in file-path, the type in resource-type, and an ID in resource-id, this function creates a new resource whose value is binary-data. If the optional resource-name parameter is defined, the new resource will be named as specified.

See Also:

appendFile ( file-path, text )
Returns: null Location: FileLib
ParameterDescription
file-pathfull path to file
texttext to append to the file

Appends the value of the text parameter to the end of a text file. This function is very useful for creating log files apart from WebSiphon's main log or exporting data in a format usable by other applications. Remember to include the appropriate number of tabs, carriage returns, etc. for formatting if necessary.

The output file will be created if no file exists as specified by file-path.

Example:

my_log = template_path & "my log.log";
appendFile(my_log, "\rThis template was accessed. Yow!");
copyFile ( file-path, new-path )
Returns: null Location: FileLib
ParameterDescription
file-pathfull path to file
new-pathnew path to copy file to

Copies the file whose full path is file-path to the new location specified by new-path. The new-path parameter may be a destination folder path or full file path.

This function will only work with files, not folders. If you need to copy a folder, use copyFolder() instead.

copyFolder ( folder-path, new-path )
Returns: null Location: FileLib
ParameterDescription
folder-pathfull path to folder
new-pathnew path to copy folder to

Copies the folder whose full path is folder-path to the new location specified by new-path. The new-path parameter must be a folder path, not a path to a file or an error will be returned.

This function will only work with folders, not files. If you need to copy a file, use copyFile() instead.

countResources ( file-path, resource-type )
Returns: integer Location: FileLib
ParameterDescription
file-pathfull path to file
resource-typethe resource type

Given the full path to a resource file in file-path and the resource type in resource-type, this function returns the number of resources of the specified type that exist.

fileExists ( file-path )
Returns: boolean Location: FileLib
ParameterDescription
file-pathfull path to file

Given the full path file-path, a boolean will be returned indicating whether the file actually exists or not.

Examples:

  1. if fileExists("Hard Drive:System Folder:Finder") then
      print "true";
    else
      print "false";
    end if;
    
    >> true
  2. // Look for a data file and if it does not exist create it.
    if not(fileExists(template_path & "Browser.data")) then
      makeFile(template_path & "Browser.data");
    end if;
folderExists ( folder-path )
Returns: boolean Location: FileLib
ParameterDescription
folder-pathfull path to folder

Given the full path folder-path, a boolean will be returned indicating whether the folder actually exists or not.

Example:

if folderExists("Hard Drive:System Folder:") then
  print "true";
else
  print "false";
end if;

>> true
getCreationDate ( file-path )
Returns: string Location: FileLib
ParameterDescription
file-pathfull path to file

Given the full path file-path, the file's creation date and time will be returned as a string containing both parts in short form. This string is suitable for passing to the date and time functions.

getFileComments ( file-path )
Returns: string Location: FileLib
ParameterDescription
file-pathfull path to file

Given the full path to a file or folder in file-path, this function returns the file or folder's comments (as displayed in the Finder's Get Info window).

getFileCreator ( file-path )
Returns: string Location: FileLib
ParameterDescription
file-pathfull path to file

Given the full path to a file in file-path, this function will return its creator string.

Example:

print getFileCreator(template_path & template_name);

>> R*ch
getFileSize ( file-path )
Returns: integer Location: FileLib
ParameterDescription
file-pathfull path to file

Given the full path in file-path, the file's size in bytes will be returned as an integer. The size is based on the data fork only, so any resources will not be included in the returned file size.

Example:

log_size = getFileSize(site_path & "WebSTAR.log");
print "WebSTAR log file is " & (log_size / 1024 as float(,2)) & "K.";

>> WebSTAR log file is 465K.
getFileType ( file-path )
Returns: string Location: FileLib
ParameterDescription
file-pathfull path to file

Given the full path to a file in file-path, this function will return its file type as a string.

Example:

print getFileType(template_path & template_name);

>> TEXT
getFolderContents ( folder-path )
Returns: list Location: FileLib
ParameterDescription
folder-pathfull path to folder

Given a full path to a folder in folder-path, a list of its contents as full paths will be returned. Folders in the list are returned with a colon appended to their name, as in "Folder:". The isFolder() function is an alternative method of detecting whether or not an item is a folder.

Example:

print getFolderContents(template_path);;

>> Hard Drive:Web Site:file_lister.t
Hard Drive:Web Site:images:
getModDate ( file-path )
Returns: string Location: FileLib
ParameterDescription
file-pathfull path to file

Given the full path file-path, the file's modification date and time will be returned as a string containing both parts in short form. This string is suitable for passing to the date and time functions.

Example:

// Get the template's modification date.
print getModDate(template_path & template_name);

>> 7/5/96 10:16:27 PM
getResource ( file-path, resource-type, resource-ref )
Returns: binary Location: FileLib
ParameterDescription
file-pathfull path to file
resource-typethe resource type
resource-refthe resource name or ID

Given a resource type in resource-type and an individual name or ID in resource-ref, this function will return the contents of the resource as a binary object. The full path to the resource file is specified in file-path.

See Also:

getResourceID ( file-path, resource-type, resource-number )
Returns: integer Location: FileLib
ParameterDescription
file-pathfull path to file
resource-typethe resource type
resource-numbersequential resource number

Given a resource type in resource-type and a sequential number in resource-number, the resource's ID is returned. The full path to the resource file is specified in file-path.

getStringResource ( file-path, resource-ref [, index ])
Returns: string Location: FileLib
ParameterDescription
file-pathfull path to file
resource-refstring resource name or ID
[index]the string index number ("STR#" resources) [optional]

Returns the contents of the "STR " resource whose ID is resource-ref. If an optional index is provided, the appropriate string from a "STR#" resource will be returned. The full path to the resource file is specified in file-path.

isAlias ( file-path )
Returns: boolean Location: FileLib
ParameterDescription
file-pathfull path to file

Given the full path to a file or folder in file-path, a boolean will be returned indicating whether or not the path points to an alias.

Example:

  1. if isAlias(template_path) then
      print "true";
    else
      print "false";
    end if;
    
    >> false
  2. if isAlias(template_path & "test_alias.t") then
      print "true";
    else
      print "false";
    end if;
    
    >> true
isFolder ( file-path )
Returns: boolean Location: FileLib
ParameterDescription
file-pathfull path to file

Given the full path to a file in file-path, a boolean will be returned indicating whether or not the path points to a folder.

Example:

  1. if isFolder(template_path & template_name) then
      print "true";
    else
      print "false";
    end if;
    
    >> false
  2. if isFolder(template_path & "images") then
      print "true";
    else
      print "false";
    end if;
    
    >> true
makeAlias ( file-path, alias-path )
Returns: null Location: FileLib
ParameterDescription
file-pathfull path to file
alias-pathfull path to alias

Given the full path to an existing file in file-path, this function creates an alias to the file at the location specified as a full path in alias-path. The alias-path parameter may be a folder destination path or a full file path.

makeFile ( file-path [, file-creator, file-type ])
Returns: null Location: FileLib
ParameterDescription
file-pathfull path to file
file-creatorfile creator
file-typefile type

Creates a new, empty file whose full path is file-path. The file's creator is specified using the optional file-creator and file-type parameters. If file-creator or file-type is not specified they will be set to BBEdit ("R*ch") and plain text ("TEXT"), respectively.

Example:

  1. // Creates a new text file, with default creator and type.
    makeFile(template_path & "Browsers.data");
  2. // Creates a new text file in the same directory as the running 
    // template with the name "Browsers.data". The creator is set to 
    // TeachText.
    makeFile(template_path & "Browsers.data", "ttxt", "TEXT");
makeFolder ( folder-path )
Returns: null Location: FileLib
ParameterDescription
folder-pathfull path to folder

Creates a new folder whose full path is specified in folder-path.

moveFile ( file-path, new-path )
Returns: null Location: FileLib
ParameterDescription
file-pathfull path to file
new-pathnew full path to move file to

Moves the file whose full path is file-path to the new location specified by new-path. The new-path parameter may be a destination folder path or full file path.

This function will only work with files, not folders. If you need to move a folder, use moveFolder() instead.

moveFolder ( folder-path, new-path )
Returns: null Location: FileLib
ParameterDescription
folder-pathfull path to folder
new-pathnew full path to move folder to

Moves the folder whose full path is folder-path to the new location specified by new-path. The new-path parameter must be a folder path, not a path to a file or an error will be returned.

This function will only work with folders, not files. If you need to move a file, use moveFile() instead.

pathToFileName ( file-path )
Returns: string Location: FileLib
ParameterDescription
file-pathfull path to file

Given a full path to a file in file-path, the file name itself will be returned with no path. If file-path actually points to a folder, the name of the folder will be returned.

Example:

print template_path & template_name & "\r";
print pathToFileName(template_path & template_name);

>> Hard Drive:Web Site:admin:banner_admin.t
   banner_admin.t
readBinaryFile ( file-path )
Returns: binary Location: FileLib
ParameterDescription
file-pathfull path to file

Reads the entire file whose full path is file-path as a binary object.

Example:

// Read in a GIF image file and return it to the browser.
the_image = readBinaryFile(template_path & ":images:ball.gif");
setMIMEType("image/gif");
return the_image;

See Also:

readFile ( file-path [, length, offset ])
Returns: string Location: FileLib
ParameterDescription
file-pathfull path to file
[length]length to read in bytes [optional]
[offset]starting at offset [optional]

This function reads a text file whose full path is specified by file-path, starting either at the beginning of the file or offset if it is defined, and continuing through to the end of the file or length bytes total.

Because the file is read directly into memory, you may need to read large files in smaller chunks or increase WebSiphon's memory allocation.

The fourth example below demonstrates how to import a tab-delimited text file into a Verona database.

Example:

  1. // Read entire data file
    the_file = readFile(template_path & "Browsers.data");
  2. // Read first 20K of data file
    chunk = (20 * 1024);
    the_file = readFile(template_path & "Browsers.data", chunk);
  3. // Read second 20K of data file
    chunk = (20 * 1024);
    offset = chunk_size * 1;
    the_file = readfile(template_path & "Browsers.data", chunk, offset);
  4. // This final example reads in the data file (which is
    // tab-delimited) and imports its contents into a Verona 
    // database.
    file_contents = readFile(template_path & "Browsers.data");
    file_contents_list = split(file_contents, "\r");
    repeat with the_entry in file_contents_list
      the_entry = split(the_entry, "\t");
      vAddRecord("Browsers", [["name" the_entry'1]
                              ["vers" the_entry'2]]);
    end repeat;
removeFile ( file-path )
Returns: null Location: FileLib
ParameterDescription
file-pathfull path to file

Premanently deletes the file whose full path is file-path.

Warning: This operation is not reversible!

removeFolder ( folder-path )
Returns: null Location: FileLib
ParameterDescription
folder-pathfull path to folder

Premanently deletes the folder whose full path is folder-path. Any items contained within the specified folder will be deleted as well!

Warning: This operation is not reversible!

removeResource ( file-path, resource-type, resource-ref )
Returns: null Location: FileLib
ParameterDescription
file-pathfull path to file
resource-typethe resource type
resource-refthe resource name or ID

Given the full path to a resource file in file-path, the type in resource-type, and an ID or name in resource-ref, this function permanently deletes the resource.

Warning: This operation is not reversible!

resolveAlias ( alias-path )
Returns: string Location: FileLib
ParameterDescription
alias-pathfull path to alias

Given the full path to an alias in alias-path, this function will return the full path to the original file the alias points to. If alias-path is not an alias, the returned value will be the same as was passed in.

returnBinaryFile ( file-path [, mime-type ])
Returns: binary Location: FileLib
ParameterDescription
file-pathfull path to file
[mime-type]specify MIME type [optional]

Streams the entire file whose full path is file-path as a binary object back to the template. Because the file is streamed then the entire file is not loaded into memory first.

The file's MIME type is specified as a string using the optional mime-type parameter. If mime-type is not specified it will use the default type as configured in the web server application.

Example:

  1. // Read entire data file
    returnBinaryFile(template_path & ":images:ball.gif", "image/gif");
  2. // This example yields the same results in a different manner. 
    // Read in a GIF image file into memory and return it to the browser.
    the_image = readBinaryFile(template_path & ":images:ball.gif");
    setMIMEType("image/gif");
    return the_image;

See Also:

setFileComments ( file-path, string )
Returns: null Location: FileLib
ParameterDescription
file-pathfull path to file
stringany text string

Given the full path to a file or folder in file-path, this function sets the file or folder's comments (as displayed in the Finder's Get Info window) to string.

setFileCreator ( file-path, new-creator )
Returns: null Location: FileLib
ParameterDescription
file-pathfull path to file
new-creatornew file creator

Given the full path to a file in file-path, this function sets its creator to the one specified in new-creator.

setFileName ( file-path, filename )
Returns: null Location: FileLib
ParameterDescription
file-pathfull path to file
filenamenew file name

Given the full path to a file in file-path, this function will change the name of the file to filename. The new filename must be a string and not a path.

Example:

log_file = site_path & "WebSTAR.log";
setFileName(log_file, "Old WebSTAR.log");
setFileType ( file-path, new-type )
Returns: null Location: FileLib
ParameterDescription
file-pathfull path to file
new-typenew file type

Given the full path to a file in file-path, this function will change the file's type to the one specified in new-type.

setResource ( file-path, resource-type, resource-ref, binary-data )
Returns: null Location: FileLib
ParameterDescription
file-pathfull path to file
resource-typethe resource type
resource-refthe resource name or ID
binary-databinary value of resource

Given the full path to a resource file in file-path, the type in resource-type, and name or ID in resource-ref, this function replaces the resource value with binary-data.

See Also:

writeBinaryFile ( file-path, binary-data )
Returns: null Location: FileLib
ParameterDescription
file-pathfull path to file
binary-databinary data to write to the file

Writes the value of the binary-data parameter to a file. Any previous contents of the file specified will be replaced, so if you wish to modify a file, you must first read it with readBinaryFile().

The output file will be created if no file exists as specified by file-path.

See Also:

writeFile ( file-path, text )
Returns: null Location: FileLib
ParameterDescription
file-pathfull path to file
texttext to write to the file

Writes the value of the text parameter to a text file. Any previous contents of the file specified will be replaced, so if you wish to modify a file, you must first read it with readFile().

The output file will be created if no file exists as specified by file-path.

Example:

  1. // Put the name of the last-run template in a file.
    writeFile(the_file, template_name);
  2. // Replace all CRLFs with CRs.
    contents = readFile(the_file);
    contents = replace(contents, "\r\n", "\r");
    writeFile(the_file, contents);

See Also:

contents prev next

Copyright (c)1996-2003 Purity Software