Reusable projects and mass operations with files

The fact that one project can run (call) another project means that projects can be re-usable. Let's get back to our analogy with Alice and Bob again:

If Alice can ask Bob to deliver letters, so can Pete. Alternatively, we can generalize Bob's role by introducing one more parameter for Bob — Item. Now Bob can deliver whatever item to any address. Therefore he can deliver, for instance, letters for Alice, and parcels for Pete.

One of the benefits of reusable projects is that they allow encapsulating a processing logic. Therefore, if a change of the logic is required it can be done in one place only. If, for instance, you have a reusable project that archives files using 7-zip, you can switch to using Winrar by changing external application path in that reusable project only, without changing any other project that requires file compression.

Readers familiar with programming might notice at this point that EasyMorph projects are effectively subroutines in terms of imperative programming, or functions in terms of functional programming. Project parameters effectively serve as function (subroutine) arguments. Result tables are returned values (when applicable).

Mass operations with files

One of the common use cases for reusable projects is mass operations with files. Let's assume we have a project that uses "Run Program" transformation to copy one file from one location to another location. The Old location and the new location are specified by parameters.

Note that since it's a Windows shell command we may avoid specifying cmd.exe as the application to run (although it was required in older versions of EasyMorph). The command line is calculated as an expression which can be seen inside the red rectangle in the picture above.

Hint: Wrap file names in double quotes in command line expressions as they may contain spaces.

Now, if we save this project as, say, file_copy.morph we obtain a reusable generic project that can be called from other projects whenever we need to copy a file. All that the calling project has to do is provide old and new locations for each file that has to be copied. Typically reusable projects are used in iterations.

Hint: If you just need to run an external project once use iteration for a table with 1 row, or use "Call" transformation available starting from version 3.0.

Following this example you can have a library of reusable projects for various file operations like deleting, moving or renaming files. Typical use cases for mass operations with files include:

  • Copying or deleting files in a list
  • Moving files from a folder to different destination folders based on file name, size or extension
  • Renaming files using complex rules that can include, for instance, retrieving a date from a file's content and then using it for a new name of the file
  • File encoding/decoding

Mass operations with files without re-usable projects

A simpler (but not re-usable) way to perform mass operations with files is to use "Iterate program" transformation. In this case no re-usable projects are created. Instead the command line is calculated as an expression right in "Iterate program" transformation for each line of a table with file names.

Hint: To see what a calculated command line would look like before iterating external program you can pre-calculate command lines as a new column.


Read next: Data analysis