hub.eb?material_id=115&track_id=414

File Services


The services.file interface contains functions for interacting with files on the server or Service Plan. Using File Services it is possible to read, write and manipulate files in the file system.

The available functions include:

  • readFile(), for reading the contents of a file
  • writeFile(), for creating or overwriting the contents of a file
  • createNewFile(), for creating new files
  • createNewDirectory(), for creating new directories
  • moveFile(), for moving files
  • deleteFile(), for permanently removing files from the filesystem
  • existsFile(), used to check the existence of a file or directory
  • isFile() and isDirectory(), used to determine the type of a file path
  • getUserDataApplicationPath(), returns the root directory of the application's file system
  • getWebAppRootFilePath(), returns the public directory containing server resources

A common use for this interface is to manage files uploaded by end users of an application.

Steps


1

Create a new form and add a Button Control with the text Move file.

2

Add an On Click script for the button. Add the following code to create a file and move it:

// Store the original file path
var fromDir = services.file.getUserDataApplicationPath();
var fromPath = fromDir + services.file.getSeparator() + "myFile.txt";

// Check whether there is a file to move and create one if not
if (!services.file.existsFile(fromPath)) {
  services.file.writeFile(fromPath, "Hello, world.")
}

// Define a destination directory to move the file to.
var destDir = fromDir + services.file.getSeparator() + "myNewDir";

// Define the location for the new file
var destPath = destDir + services.file.getSeparator() + "myMovedFile.txt";

// Check whether the destination directory exists and create it if not
if (!services.file.existsFile(destDir)) {
  services.file.createNewDirectory(destDir);
}

// Move the file!
services.file.moveFile(fromPath, destPath);
3

Run the form and click the button. A text file will have been created inside your application directory (on Windows it will be similar to C:\Users\USERNAME\Verjio\Studio\embeddedServer\app\). The file will then have been moved to a newly created directory, myNewDirectory.

Current Module

Related