About EasyMorph Tutorials & Examples Web-help

Make Copy of Files and Save Copies at a Designated Location


#1

Hi Dmitry,

I’m trying to achieve the following tasks in one project, and was wondering a way to do this:

I have 5 data sources that are saved at 5 different folder and that are being updated on a daily basis. Everyday, within each data source, one file is generated with a fixed file name plus a date stamp at the end. I’m hoping to make a copy of the most recently dumped file within each data source folder and save the copies at 5 designated folders (that are different from the original location of the 5 data sources).

Thanks very much for your help!

Lianne


#2

Hi Lianne and welcome to the Community,

My answer is a bit lengthy, so I will split it into two parts.

1. Project that copies most recent file

Below is a project that copies the most recent file in one folder into another folder:

copy_latest_file.morph (2.4 KB)

Here is how it works:

The project has 2 parameters: “Source folder” and “Target folder”.

Parameters are edited through the Parameter Editor (go to menu Project -> Edit parameters, or press F6). See this tutorial article to read more on parameters: Tutorial: Project parameters.

There are 4 transformations in the project (see below).

A few notes on the transformations:

Step 1: Generate list of files in {Source folder}.

This transformation simply generates a list of files in specified folder. Note that here we use one of the project parameters to specify the folder in the transformation:

Step 2 and Step 3
Self-explanatory.

Step 4: Iterate Windows shell command

This transformation runs a Windows command once per each line in the table. But since in Step 3 we kept only 1 top row in the table the command will be executed only once.

The Windows command copies a file from one folder to another folder and is defined by the following EasyMorph expression (you can see it in the transformation settings):

'copy /y "' & [File name with full path] & '" "' & {Target folder} & '"'

It might look a bit cryptic, but let me explain what it does:

& - this is the text concatenation operator. It concatenates two text strings. E.g. ‘A’ & ‘B’ results in AB.

Text strings in EasyMorph expressions can be wrapped either in single quotes or in double quotes. Because both source and target file paths can contain spaces Windows requires them to be wrapped in double quotes. That’s why in the expression we put double quotes inside text strings which are in turn wrapped in single quotes. Double quotes are required by Windows. Single quotes are required by the EasyMorph expression syntax.

[File name with full path] is a reference to field. It will be replaced with actual field value.

{Target folder} is a reference to parameter. Notice that unlike a field reference, a parameter reference should be wrapped in curly braces, not square ones.

The final result of the calculated expression depends on parameters and actual folder contents. When I prepared this example, the expression evaluated to

copy /y "D:\Downloads\vc_redist.x64 (1).exe" "C:\Users\Dmitry\Desktop"

The /y option ensures that if a file with the same name already exists in the target folder it will be overwritten. Otherwise the command (and respective transformation) would fail.

Once you download the project you can change project parameters to some real folders on your computer and press F5 to run it. It should copy the latest file from the source folder to the target folder you provided.

2. Copying files from multiple folders

OK, now you have a project that can copy the most recent file from one folder to another. How do you use it?

Of course, you can run the project described above manually, changing manually the source and target folders specified in parameters. However, it’s better to automate it too. Below is a sample project that calls our first project (copy_latest_file.morph) twice for another source and target folders. See this tutorial article on calling projects in EasyMorph: Tutorial: Calling other projects.

copy_files.morph (1.9 KB)

You can modify it to add 3 more calls, and provide your actual source and target folders for all 5 calls. To run the project press F5.

PS. If you had more than 5 source folders then it would make sense to use iterations, but that would have made the already long explanation even longer. So for this case just repeating 5 calls transformations would suffice.


#3

Wow! Thank you so much for the detailed explanation. Your respond speed is impressive!