So I spent a bit more time completing the script, which now looks like this:
import glob
import os
import shutil
import xml.etree.ElementTree as ET
tree = ET.parse(‘/home/patrick/Sources/CopyFiles/Source/layers.qlr’)
root = tree.getroot()
for layer in root.iter(‘layer-tree-layer’):
sourcename = layer.get(‘name’)
print(“Layer: ” + sourcename)
sourcepath = “/home/patrick/Sources/CopyFiles/Source/” + sourcename + “.*”
#print(fpath)
for file in glob.glob(sourcepath):
#print(file)
destname = os.path.basename(file)
#print(destname)
destpath = “/home/patrick/Sources/CopyFiles/Dest/” + destname
#print(destpath)
print(“Copying ” + file + ” to ” + destpath)
shutil.copyfile(file,destpath)
So there are a few commented out lines which we can ignore, the key parts are that it:
- Imports the contents of a qlr file as XML and parses it
- Gets the layer names from the XML and makes up a wildcard file spec to copy
- Copies the files from the source to the destination.
python copyfiles.py
And it works exactly as expected.
I have also created a second version of the script called movefiles.py, that is the same except it uses the shutil.move function to move the file instead of copying it. This is especially useful where you need to split up an existing directory’s contents.