Check if a Path Exists

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

Home >  Job Configuration Examples >

Check if a Path Exists

This example shows you how to create a job which informs you if a path (to a file or directory) exists on the operating system. To achieve this goal, you will use a combination of built-in functions and expression functions. The job will be defined as a Web service, so that you can trigger it on demand, by accessing a URL from the browser. The job will take the path as an argument, and will return a string which informs whether the path supplied as argument exists on the operating system where FlowForce Server runs.

 

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).

 

Creating the job

1.Log on to FlowForce Server and navigate to a container where you have permission to create new jobs (in this example, we are using /public).
2.Click Create, and then select Create job.
3.Add a job name (CheckPath, in this example) and, optionally, a job description.

ExamplePathExists01

4.Under Job Input Parameters, click add, and add the parameter path, as shown below.

ExamplePathExists02

5.Add a new execution step which calls the builtin_function/system/shell/commandline function, and enter the shell command which checks for the existence of the file. Make sure to declare the result of this step, as shown below (in this example, we called it output).

ExamplePathExists03

On Windows, the shell command outputs "1" when the path exists and "0" when it does not exist. If FlowForce Server runs on a Unix system, adjust the command accordingly. Notice that the command embeds the FlowForce expression {path}. This expression references the input parameter defined in the previous step.

6.Under "Execution Steps", click the add button, and then select new Choose step. Then enter trim(content(stdout(output))) == '1' as condition expression. This expression consists of three nested functions: stdout, content, and trim. First, the stdout function gets the standard output of the result returned by the previous step. Then the content function converts the standard output to string. Finally, the trim function removes any leading or trailing spaces, carriage returns, or line feeds from the standard output. The result is then compared to "1" using the equality operator. If both values are equal, the path exists. Otherwise, the path does not exist.
7.Under the When clause, add an execution step as shown below. This execution step calls the builtin_function/system/compute-string function to build the string value that should be returned when the path exists. Notice that the value embeds the FlowForce expression {path}. This expression references the input parameter defined in a previous step.

ExamplePathExists04

8.Under the Otherwise clause, add an execution step as shown below. This execution step calls the builtin_function/system/compute-string function to build the string value that should be returned when the path does not exist. Notice that the value embeds the FlowForce expression {path}. This expression references the input parameter defined in a previous step.

ExamplePathExists05

9.Under Execution Result, declare the return type as string.

helloworld04

10.Under Service, click to select the Make this job available via HTTP check box, and enter CheckPath as name of the service (see Exposing Jobs as Web Services ).
11.Under "Credentials", select an existing credential record or specify a local credential (see Credentials ).
12.Click Save.

 

Running the job

To test the job, enter http://127.0.0.1:4646/service/CheckPath 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 (see Setting the Network Address and Port ), change the address accordingly. If prompted for credentials when accessing the Web service, supply the same credentials you use to log on to FlowForce Server.

 

If you use Internet Explorer to test FlowForce Server jobs exposed as Web services, you may need to disable the "Show friendly HTTP error messages" option in the Advanced tab.

 

Since this job has arguments, you will be prompted to supply them when you access the Web service in the browser.

ExamplePathExists06

If the job executes successfully, the browser displays the output of the job, for example:

 

Path C:\ exists.

 

If the job fails, the browser displays a "Service execution failed" message. In this case, check the log of the job in FlowForce Server to identify the error (see Viewing the Job Log).


© 2019 Altova GmbH