Skip to main content
Announcements
SYSTEM MAINTENANCE: Thurs., Sept. 19, 1 AM ET, Platform will be unavailable for approx. 60 minutes.
hic
Former Employee
Former Employee

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