# How to link to external files with relative path in QGIS actions?

Objective: I have point layers with attributes contain paths to external files (visio, autocad dwg...). I want to put them all in one folder together with the project file, compress the folder into a zip file and send it to my colleagues for reviews. When my colleague de-compress the zip file, they would be able to open those files with simple actions like the posts linked below.

My observation: what I want to accomplish is very similar to those discussed in this post and this post with one difference: when the zip file is de-compressed, absolute path to referenced files will be different. But I think that RELATIVE PATHS to the project file will be the same, so why not use this? The problem is: I can not find a way to open external files with relative paths inside QGIS.

Any idea how to solve this problem?

Thanks a lot!

You could try the eVis plugin for QGIS

http://biodiversityinformatics.amnh.org/open_source/evis/documentation.php#d0e390

Thanks to Rayner's answer, I can do what I want now - with some things to remember:

1. The relative path stored as attribute in data table should use forward slash (/), like: '/subfolder/file.ext' (without apostrophes) because my Linux Mint system won't work with back slash (\) while Windows XP can work with both styles.
2. The base path specified in eVis' Options tab should use your operating system's convention, that is: 'C:\path\to\project\folder\' (without apostrophes) on Windows and '/home/user/path/' (without apostrophes) on Linux.

Otherwise I will receive an alert that the file could not be found.

There is another solution for portable project with relative path for any attachment. With this solution you can open any document using system default browser/viewer.

• Create new Action
• Select type - python
• Paste code bellow to the Action text area
• Replace the_name_of_culumn_with_relative_path with the name of your column
import subprocess, os
import os.path;
import subprocess;
from PyQt4.QtGui import *;
filerelpath = "[% <the_name_of_culumn_with_relative_path> %]";
if (filerelpath is not None and len(filerelpath) > 0):
projectpath = qgis.core.QgsProject.instance().fileName();
path = os.path.dirname(str(projectpath)) ;
file = os.path.abspath(os.path.join(path, filerelpath));
if sys.platform.startswith('darwin'):
subprocess.call(('open', file));
elif os.name == 'nt':
os.startfile(file);
elif os.name == 'posix':
subprocess.call(('xdg-open', file));
else:
msg = QMessageBox()
msg.setText('No attachment');
msg.show();