Copy Files

www.altova.com Print this Topic Previous Page Up One Level Next page

Home >  Job Configuration Examples >

Copy Files

This example shows you how to copy multiple files on the local file system with the help of a FlowForce Server job.

 

Let's assume that you would like to copy all the files from directory C:\Source to a new directory C:\Target. (On a UNIX system, please adjust the paths accordingly.) To achieve the goal, we will use a "for-each" step that iterates through all the files in a directory, and then invoke the /system/filesystem/copy function for each item in the loop.

 

Prerequisites

Required licenses: FlowForce Server
FlowForce Server must be running at the configured network address and port (see Setting the Network Address and Port)
You have a FlowForce Server user account with permissions to one of the containers (by default, the /public container used in this example is accessible to any authenticated user).
This job copies files from directory C:\Source to directory C:\Target. Make sure to create these directories on the local file system before creating the job. Also, make sure that the source directory contains a few files to test the job.

 

Creating the job

Log on to FlowForce Server Administration Interface, and click Configuration. Open the /public container and create a new job. Enter a job name (for example, "CopyFiles"), and, optionally, a job description.

ff_example_copy_01

In order to iterate over items in a list, FlowForce Server provides a "for-each" execution step. Such a step iterates over a sequence (list) of items up to and including the last item in the sequence. In this example, our sequence of items will be the list of files in the source directory. To create the required list, click New Execution Step and type /system/compute next to "Execute function". You can also select this path from the drop-down list, as illustrated below.

ff_example_copy_02

Next, enter the following expression in the Expression field:

 

list-files("C:\Source\*.*")

 

Next, enter a name for the list in the Assign this step's result to field (in this case, the name is list). This makes it possible to easily refer to the newly created list of files in a subsequent step. Your first execution step should now look as follows:

ff_example_copy_03

The expression above invokes the list-files expression function. The function takes a path as argument (in this case, C:\Source\*.*) and returns the list of files (or directories) at the given path. Notice that the path contains the wildcard *.* to select all the files in the directory. If necessary, you can adjust the wildcard to select only specific file extensions, for example *.txt. For more information about expressions in FlowForce, see The FlowForce Expression Language.

 

You can now proceed to creating the actual "for-each" iteration step. Click New For-Each step and type list in the "in sequence" box. (This refers to the list created in the previous execution step.)

ff_example_copy_04

Tip: You could also copy the expression list-files("C:\Source\*.*") to the "in sequence" box of the "for-each" step and thus get rid of the first execution step altogether.

 

Next, click the add button and add a new execution step inside the "for-each" step. This step will invoke the /system/filesystem/copy function for each item in the loop, as illustrated below.

ff_example_copy_05

As shown above, the copy function is called with the following arguments:

 

The Source is the current item (file) in the loop. You can either type {item} in the Source box or click the ff_ic_set_to button and select item.
The Target is the target path. In this example, the path is entered as is; however, you could also supply it as an argument to the job.
The Overwrite option is enabled, meaning that if a file with the same name already exists in the source directory, it will be overwritten. To prevent this from happening, click the delete button.

 

For the sake of simplicity, we will not set the other two arguments in this example. For further information, see the description of the /system/filesystem/copy function.

 

The job created so far now includes all the required processing steps, but it has no trigger yet. To trigger the job at recurring time intervals, you could use a timer trigger, see Timer Triggers. Or you can monitor the source directory for changes and trigger the job by means of a file system trigger, see File System Triggers. Finally, you can trigger the job on demand, as a Web service call.

 

In this example, we will trigger the job on demand, by clicking a URL in the browser (in fact, this invokes the job as a Web service). To turn the job into a Web service, select the Make this job available via HTTP... check box and enter the name of the Web service.

ff_example_copy_06

Finally, the job needs your credentials to run. Therefore, enter your operating system username and password (not your FlowForce Server username and password) in the "Credential" section, as shown below. Alternatively, if you created standalone credentials previously, as described in Defining Credentials, you can select them using the Select existing credential option.

fs_deploy_mapping_05

A credential record is the combination of user name and password associated with a user account on the operating system where the FlowForce Server job runs. When you define a job in FlowForce Server, you must supply the credentials with which the job must be executed. Note that if the user account associated with the supplied credentials does not have sufficient rights on the operating system, the job cannot execute successfully.

 

Running the job

To test the job, enter http://127.0.0.1:4646/service/CopyFilesService in the browser's address bar, provided that FlowForce Server runs at the default host and port name. If you have defined other host and port settings in the Configuration page, change the address accordingly, as described in Setting the Network Address and Port. If prompted for credentials when accessing the Web service, supply the same credentials you use to log on to FlowForce Server.

 

Upon successful execution, the job will copy all the files from the source to the target directory. Otherwise, a "Service execution failed" error is displayed in the browser. If you see this error, check the log of the job for further information, see Viewing the Job Log. Possible causes may include incorrect credentials, incorrect file paths, insufficient permissions on the file system, and others. For example, the job fails if the Overwrite check box is not selected and the target directory already contains a file with the same name, as illustrated below:

ff_example_copy_07


© 2019 Altova GmbH