Qlik Community

Qlik Design Blog

All about product and Qlik solutions: scripting, data modeling, visual design, extensions, best practices, etc.

Announcements
QlikWorld, June 24-25, 2020. Free virtual event for DI and DA gurus. Register Now

When creating a load script, you often use data from files, e.g. xls, csv, txt, or qvd. By default, absolute paths to the folders are generated when the library is created. But what if you want to use relative paths? What do you do then?

Relative paths are extremely useful. They allow you to create solutions that are robust, yet portable. They also allow you to create re-usable pieces of script without hard-coding physical file locations or library names too much. 

Imagine for instance that you have many source files in a tree-like directory structure. It is easy enough to create a script using absolute paths, but what if you want to move the entire file structure to a different location? Or rename the library? The paths in the script would no longer be valid. You could in principle make a search-and-replace in all affected scripts, but it is easier if you use relative paths in the first place.

Further, you may want to use “Include” statements to refer to files containing pre-defined script entities. These could in principle contain explicit library references, but it would soon get very convoluted: First you need the library name in your Include statement – to refer to the script file itself – and then you would also need to use the library names inside the script file. No, better then to use relative paths and make the script files portable.

So, how do you use relative paths?

Directory statement

One solution is to use a “Directory” statement. It specifies a root folder that is used for all subsequent file references. This means that it is possible to use relative paths if files are stored in the root folder or in one of its sub-folders.

In the following script, both the Include statements and the Load statements use relative paths. (The Include statements load the scripts for some standardized components like environment variables and calendar.)

Directory statement.png

This method works both in QlikView and in Qlik Sense.

 

Path relative the qvw file (QlikView only)

In QlikView there is a second way to use relative paths: If no Directory statement is used, the location of the qvw file is used as root folder. This means that relative paths work directly if files are stored in the same folder as the qvw or in a sub-folder.

Folders.png

 

In the following script, both the Include statements and the Load statements use relative paths.

Relative qvw file.png

Note that the file paths contain the names of the sub-folders, without being absolute paths. Note also that you don’t need to use backward slashes – you can always use forward slashes, although the file references refer to files on a Windows drive.

This method, however, does not work in Qlik Sense.

 

Using variables

A third way is to use a variable containing the path to the root folder:

Variable.png

The structure is similar to when you use a directory statement. The difference is that here you need to dollar-expand the variable in all paths. Strictly speaking, relative paths are not used here – the dollar expansion creates an absolute path. But the solution allows you to create scripts that are flexible and portable, since only the relative path is hard-coded.

This method works both in QlikView and in Qlik Sense.

To summarize: Use relative paths to make your scripts more manageable.

HIC

4 Comments
Contributor III
Contributor III

Hi @Henric_Cronström ,

Thank you for the good post!

What is your opinion on the method with double dots and forward slash

e.g. $(Include=../Scripts/Environment.qvs);

Is this safe to use? Of course it is only for QlikView

 

In Qlik Sense I usually use the method with variables.

However will keep in mind the option with Directory statement.

0 Likes
762 Views

The method with double dots works fine in most cases. But it does give the user a way to access folders "outside" the allowed library. So in Qlik Sense there are built-in limitations on how this can be used - for safety reasons.

0 Likes
645 Views
Partner
Partner

Very simple but interisting post, thank you

214 Views
Partner
Partner

Thanks Henric,

I like these articles about these helpful little features. I had forgotten about the Directory command. These days, I create a variable for each of the loads, e.g. vCustomerLoadPath, vOrderLoadPath. Then I can just go to a single place to control where the data is coming from.

Cheers, Barnaby.

128 Views
Labels