8

I have thousands of pdf's that have meaningless names, and are scattered across several folders. I need to rename them, and fortunately most of them are referenced through links in spatial layers, where the attribute tables have meaningful information about the files and a column called 'hotlinks' has a directory path pointing to the pdf's.

Is there a method in QGIS that can follow the paths in the attribute tables, and rename the pdf's in that path based on the attributes in the layer? I will have a new field that contains the new name, eg. concat("Field1","Field2","Field3"), so I'd like to rename the pdf's based on that.

The catch...the pdf's are scattered around and are not in the same folder, so the script can't just point to one folder.

  • do the meaningless names of PDFs exist in attributes of shape files or shape files are in same path of the PDFs ? – Shiko Feb 17 '17 at 3:26
  • The meaningless names do exist in the attributes, at the end of the directory link in the link field. Eg. c:\folder\123456.pdf – Bill Feb 17 '17 at 4:31
  • so if you have the source (meaningless) and target (meaningful) in same record, then you can try my answer below – Shiko Feb 17 '17 at 4:34
15

You have 2 options:

1) Using PyQGIS + OS library: [Must run QGIS as Administrator to rename files]

import os #needed to replace filenames
layer = iface.activeLayer() #get selected layer
selection = layer.selectedFeatures()
iface.activeLayer().selectAll()
for feature in selection:    
    if os.path.exists(feature['source']):
        os.rename(feature['source'], feature['target']) #rename files

2) Using Python + external library:
Use below library to read the dbf file + import os library:

import os #needed to replace filenames
from dbfread import DBF #read dbf file
for record in DBF('MyFile.dbf'):
   if os.path.exists(record['source']):
      os.rename(record['Source'], record['target'])

Reference:
https://pypi.python.org/pypi/dbfread

  • Thanks for that. I'm not having much luck though...if I have this in the Python console and run the command, it should change any pdf's referenced in the shapefile records from c:/meaningless1.pdf to c:/meaningful1.pdf etc.? One of my shapefiles can have hundreds of pdf links too. Sorry, I'm a bit of a python noob. – Bill Feb 20 '17 at 23:00
  • Also I'm running this in the console: Python Console Use iface to access QGIS API interface or Type help(iface) for more info layer = iface.activeLayer() selection = layer.selectedFeatures() for feature in selection: os.rename(feature['source'], feature['target']) – Bill Feb 20 '17 at 23:02
  • @Bill in PyQGIS, you can enter your complete code in the Editor and run it instead of writing line by line in console – Shiko Feb 21 '17 at 1:21
  • Thanks. Still not having luck though, the script writes a python file to my c:\users.....temp folder, but doesn't rename the pdf's. I have the meaningless field down as 'source' and and the meaningful field down as 'target', would the field names be the cause? Cheers – Bill Feb 21 '17 at 4:29
  • @Bill Can you try to open the QGIS as 'Administrator' ? Do you get any error ? – Shiko Feb 21 '17 at 4:35

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.