FileOps use cases

7 minute readExtensibilityDeveloper productivity

The following use cases demonstrate common uses for the FileOps plugin.

Perform common file operations

In this example, you perform several operations:

  1. Create a directory.

  2. Create files in the directory.

  3. Create an archive from the directory.

  4. Delete the directory.

  5. Open the archive file.

Set up a use case project and procedure

Create a project, procedure, and parameter in the procedure:

  1. Create a project called Use Cases.

  2. In the Use Cases project, create a procedure called Use Case CommonFileOps.

  3. For Use Case CommonFileOps, create a new parameter called basedir with the following values:

    • Name: basedir

    • Type: Text entry

    • Required? (Checkbox): Checked

      You can leave all other fields blank.

Create directories

To use the FileOps plugin to create a directory:

  1. Create a step named Create Directory and select Definition.

  2. For the procedure Task type, select Plugin.

  3. From the plugin Select category dropdown menu, select Utility.

  4. From the Select plugin dropdown menu, select the EC-FileOps plugin, and then select the CreateDirectory procedure.

  5. Populate the Input Parameters field Path as $[basedir]/directory.

Create files in a directory

To use the FileOps plugin to create files in a directory:

  1. Create a new step named Create FirstFile and select Definition.

  2. For the procedure Task type, select Plugin.

  3. From the plugin Select category dropdown menu, select Utility.

  4. From the Select plugin dropdown menu, select the EC-FileOps plugin, and then select the CreateEmptyFile procedure.

  5. Populate the Input Parameters fields:

    • Name: $[basedir]/directory/file1.txt

    • Permission Mode: Leave blank

  6. Create a copy of the previous step, and name it Create SecondFile.

  7. Populate the Input Parameters fields:

    • Name: $[basedir]/directory/file2.txt

    • Permission Mode: Leave blank

Create an archive of a directory

To use the FileOps plugin to create an archive file of a directory:

  1. Create a new step named Zip Directory and select Definition.

  2. For the procedure Task type, select Plugin.

  3. From the plugin Select category dropdown menu, select Utility.

  4. From the Select plugin dropdown menu, select the EC-FileOps plugin, and then select the Create Zip File procedure.

  5. Populate the Input Parameters fields as follows:

    • Source Path: $[basedir]/directory

    • Zip File: $[basedir]/directory.zip

Delete a directory

To use the FileOps plugin to delete a directory:

  1. Create a new step called Delete Directory and select Definition.

  2. For the procedure Task type, select Plugin.

  3. From the plugin Select category dropdown menu, select Utility.

  4. From the Select plugin dropdown menu, select the EC-FileOps plugin, then select the DeleteDirectory procedure.

  5. Populate the Input Parameters fields as follows:

    • Source Path: $[basedir]/directory

    • Recursive (Checkbox): Checked

Unzip a directory

To use the FileOps plugin to unzip a directory:

  1. Create a new step named UnZip Directory and select Definition.

  2. For the procedure Task type, select Plugin.

  3. From the plugin Select category dropdown menu, select Utility.

  4. From the Select plugin dropdown menu, select the EC-FileOps plugin, and then select the Unzip File procedure.

  5. Populate the Input Parameters fields as follows:

    • Zip File: $[basedir]/directory.zip

    • Destination Directory: $[basedir]

Use case summary

If you followed all the sections in this use case, your procedure should be similar to:

Figure 1. CommonFileOps procedure overview
Figure 1. CommonFileOps procedure overview

Execute the procedure and provide a directory path for the basedir parameter. In the following image, /tmp directory is used:

Figure 2. CommonFileOps procedure parameter input
Figure 2. CommonFileOps procedure parameter input
For Windows machines, change the basedir parameter to a Windows path such D:/.

After running the procedure, you should be able to see something similar to:

Figure 3. CommonFileOps procedure log
Figure 3. CommonFileOps procedure log

This procedure is portable, and you can reuse it as needed.

Download and verify a file from the internet

In this example, you will create a procedure to download a file from internet and compare its checksum with the following procedures:

  1. Download a file from the internet, such as an Ant binary.

  2. Download the file’s checksum.

  3. Compare the file with the checksum.

Set up a use case project and procedure

Create a project, procedure, and parameter in the procedure:

  1. If you do not already have one, create a project called Use Cases.

  2. In the Use Cases project, create a procedure called Use Case VerifyFile.

  3. For Use Case VerifyFile, create a new parameter called basedir with the following values:

    • Name: basedir

    • Type: Text entry

    • Required? (Checkbox): Checked

      You can leave all other fields blank.

Download a file and its checksum

You can use the Command task type to implement executable commands into your pipelines. To create a command step to download the use case file and checksum:

  1. Create a new step named Download Ant and its checksum and select Definition.

  2. For Task type, select the Command option.

  3. In the Command field, to get the binary and checksum enter:

    wget https://archive.apache.org/dist/ant/ant-current-bin.zip \ https://archive.apache.org/dist/ant/ant-current-bin.zip.md5
  4. In the Working Directory field, enter $[basedir] to use the parameter created previously.

  5. Fill the other fields as shown in the following image:

    Parameter input for Download Ant command step
    Figure 4. Download Ant command procedure input parameters

Compare checksum values

To use the FileOps plugin to compare checksums:

  1. Create a new step, name it Read Checksum, and select Definition.

  2. For the procedure Task type, select Plugin.

  3. From the plugin Select category dropdown menu, select Utility.

  4. From the Select plugin dropdown menu, select the EC-FileOps plugin, and then select the SaveFileContent procedure.

  5. Fill the fields as follows:

    • Path: $[basedir]ant-current-bin.zip.md5

    • Content (output property path): /myjob/md5

  6. Create a new step, name it Validate Checksum, and select Definition.

  7. For the procedure Task type, select Plugin.

  8. From the plugin Select category dropdown menu, select Utility.

  9. From the Select plugin dropdown menu, select the EC-FileOps plugin, and then select the Validate Checksum procedure.

  10. Fill the fields as follows:

    • File: $[basedir]ant-current-bin.zip

    • Checksum Type: MD5

    • Expected Checksum: $[/myjob/md5]

Use case summary

If you followed the sections in this use case, your procedure should be simular to:

Figure 5. VerifyFile procedure overview
Figure 5. VerifyFile procedure overview

Run the procedure and provide a path for the basedir field. The following images uses /tmp.

Figure 6. VerifyFile input parameters
Figure 6. VerifyFile input parameters
For Windows machines, change the basedir parameter to a Windows path such D:/.

After running the procedure we should see something like:

Figure 7. VerifyFile procedure log
Figure 7. VerifyFile procedure log

Job properties:

Figure 8. VerifyFile procedure job properties
Figure 8. VerifyFile procedure job properties

Perform a file diff

In this example, you create a procedure to find the differences between two files, with the following procedures:

  1. Create two different files.

  2. Compare the content in the two files and create a log showing the differences.

Set up a use case project and procedure

  1. Create a project called Use Cases.

  2. In the Use Cases project, create a procedure called Use Case FileDiff.

  3. For Use Case FileDiff, create a new parameter called basedir with the following values:

    • Name: basedir

    • Type: Text entry

    • Required? (Checkbox): Checked

Create files and add text

To create a file and add text to it:

  1. Create a new step named FirstFile, and select Definition.

  2. For the procedure Task type, select Plugin.

  3. From the plugin Select category dropdown menu, select Utility.

  4. From the Select plugin dropdown menu, select the EC-FileOps plugin, and then select the AddTextToFile procedure.

  5. Populate the Input Parameters fields as follows:

    • Path: $[basedir]/file1.txt

    • Append Mode (Checkbox): Leave unchecked

    • Add new line to the end (Checkbox): Checked

    • Content:

      This is the first line.
  6. Create a copy of the previous step, and name it SecondFile.

  7. In the Input Parameters, change the fields Path and Content as shown in the following image:

    • Path: $[basedir]/file1.txt

    • Append Mode (Checkbox): Leave unchecked

    • Add new line to the end (Checkbox): Checked

    • Content:

      This is the first line. This is the second line.

Compare files

To compare two files:

  1. Create a new step named Diff Files, and select Definition.

  2. For the procedure Task type, select Plugin.

  3. From the plugin Select category dropdown menu, select Utility.

  4. From the Select plugin dropdown menu, select the EC-FileOps plugin, and then select the DiffFiles procedure.

  5. Populate the Input Parameters fields as shown in the following image:

    • First File: $[basedir]/file1.txt

    • Second File: $[basedir]/file2.txt

    • Display Format: Table

      You can rerun the step using different display formats to find which you prefer.

Use case summary

If you followed the sections in this use case, your procedure should be simular to:

Figure 9. FileDiff procedure overview
Figure 9. FileDiff procedure overview

Run the procedure and provide a path for the basedir field. The following images uses /tmp.

Figure 10. FileDiff input parameter
Figure 10. FileDiff input parameter
For Windows machines, change the basedir parameter to a Windows path such D:/.

You can check the results on the Job Details page:

Figure 11. FileDiff procedure log
Figure 11. FileDiff procedure log

To see the result of the file diff, open the log file for Diff files:

Figure 12. FileDiff step log
Figure 12. FileDiff step log

Encrypt and decrypt files

In this example, you encrypt and decrypt a file with the following steps:

  1. Choose the file to encrypt and decrypt.

  2. Encrypt the file.

  3. Decrypt the file.

Prerequisites

  1. Create a /tmp directory.

  2. Create a file named file.txt with the following text:

    This is the first line. This is the second line.
  3. Save the file in the /tmp directory.

Set up a use case project and procedure

Set up a project and procedure to encrypt and decrypt the file:

  1. Create a project called Use Cases.

  2. In the Use Cases project, create a procedure called Use Case FileCrypt.

Encrypt files

To encrypt files:

  1. Create a new step named Encrypt File, and select Definition.

  2. For the procedure Task type, select Plugin.

  3. From the plugin Select category dropdown menu, select Utility.

  4. From the Select plugin dropdown menu, select the EC-FileOps plugin, and then select the Crypt procedure.

  5. Populate the Input Parameters fields as follows:

    • File: /tmp/file.txt

    • Action (Dropdown): Decrypt

    • Key: Enter a key you.

      Ensure you make a note of the key you enter. You must use this same key in the Decrypt files step.
    • BackUp file (Checkbox): Optional

  6. Run the procedure and open the log file in the browser:

    Figure 13. File encryption job log
    Figure 13. File encryption job log
  7. To ensure the file was encrypted, use cat file.txt to print the file in your console:

    Figure 14. Encrypted file cat example
    Figure 14. Encrypted file cat example

Decrypt files

To decrypt files:

  1. Create a new step named Decrypt File, and select Definition.

  2. For the procedure Task type, select Plugin.

  3. From the plugin Select category dropdown menu, select Utility.

  4. From the Select plugin dropdown menu, select the EC-FileOps plugin, and then select the Crypt procedure.

  5. Populate the Input Parameters fields as follows:

    • File: /tmp/file.txt

    • Action (Dropdown): Decrypt

    • Key: Enter the same key you used in the Encrypt files step.

    • BackUp file (Checkbox): Optional

Run the procedure and open the log file in the browser:

Figure 15. FileCrypt procedure log
Figure 15. FileCrypt procedure log

To ensure the file was decrypted, use cat file.txt to print the file in your console:

Figure 16. cat file results
Figure 16. cat file results

Sync two directories

In this example, the contents of two directories are synchronized.

Prerequisites

Create two folders with several files each. For the best examples, it is recommended:

  • Some files should be the same name and content.

  • Some files should have the same name but different content.

  • Some files should exist in only one of the folders.

Set up a use case project and procedure

To set up the sync folders procedure:

  1. Create a project called Use Cases.

  2. In the Use Cases project, create a procedure called Use Case SyncFolders.

Sync the directories

To sync the directories:

  1. Create a new step named Sync Folders, and select Definition.

  2. For the procedure Task type, select Plugin.

  3. From the plugin Select category dropdown menu, select Utility.

  4. From the Select plugin dropdown menu, select the EC-FileOps plugin, and then select the SyncFolders procedure.

  5. Populate the Input Parameters fields as shown in the following image:

    • First Directory: /tmp/directory1

    • Second Directory: /tmp/directory2

    • Sync Rule: Keep First

      All other fields may be left blank.

      Alternatively, you can select the Dry run parameter to simulate the actions that would be executed when the procedure runs. Dry runs are designated in the job log:

      Figure 17. SyncFolders procedure dry run log
      Figure 17. SyncFolders procedure dry run log

When you run the procedure, the step creates a log with the actions that were performed.

Figure 18. SyncFolders procedure log
Figure 18. SyncFolders procedure log