Is it possible to calculate the file name in an attribute table?

My shapefile has a name like "cities.shp". In this shapefile I want to have a attribute "cities").

  • 3
    Can you please clarify 'calculating file name'? – Joseph Feb 3 '15 at 16:09

Sorry for that stupid question! I did find an very easy way to do what I aked in this thread. Just use the QGis processing tool and the SAGA "merge vector layers" tool. The merged shape file has a column (called "Layer") filled with the shape file names.

  • Brilliant! Glad you got it sorted, remember to accept your answer after 2 days of posting (and your question wasn't stupid at all buddy!) – Joseph Feb 5 '15 at 10:20
  • 1
    OK! Hope I'll remember bud! – Pimpel Feb 5 '15 at 11:20

You can copy the following script to your QGIS (Processing Toolbox > Scripts > Tools > Create new script). This script takes shapefiles from one folder, uses the Field Calculator algorithm to add the filename as an attribute (with Null values but you can edit this to another expression) and outputs the shapefiles into another folder:


import os
import glob
from os.path import expanduser
home = expanduser("~")

#   Change to match your folder path for shapefiles
path_dir = home + "\Desktop\Test\\"
path_res = path_dir + "Results\\"

def run():

#   Set directory and search for all shapefiles then run algorithm
    os.chdir(path_dir + "Shapefiles\\")
    for fname in glob.glob("*.shp"): 

        processing.runalg("qgis:fieldcalculator", fname, fname, 2, 99, 0, True, 'NULL' , path_res  + "/"+ fname)

#   Paths of the shapefiles in the Result folder with list comprehension
    output = [shp for shp in glob.glob(path_res + "*.shp")]


There are ways to write attributes to existing shapefiles but I don't have that knowledge yet. Hope this helps!

  • I tried this but withoud success...it might be my fault, because I'm a rookie in writing scripts. What I did is I created a new script and copied your script. On my desktop I created a folder "Test" and then "Test\Shapefiles". After starting the script (it is running without errors) nothing happens and the log says "Algorithm Test starting... Algorithm Test finished" – Pimpel Feb 3 '15 at 22:28
  • OK! Now it worked but not really with the result I need. Now I have the shape-name as column name. But I need to have the name of the shape inside the column as an attribute. – Pimpel Feb 4 '15 at 10:57
  • I've tried to update the script above to write the filename into the new field created by the field calculator by altering the inputs like this, using fname as the formula: processing.runalg("qgis:fieldcalculator", fname, 'Filename', 2, 50, 0, True, fname , path_res + "/"+ fname) However, when I run it it correctly creates the 'Filename' field but it deletes all of the features in the shapefile. I assume fname isn't a suitable input, can anyone suggest anything else? (I ask only because the SAGA tools on Mac OSX are currently broken) – Ed Rollason Sep 11 '15 at 14:18
  • @EdRollason - To clarify, you want to have the name of the file as values in a column called Filename? If so, try adding this extra line after the for statement: formula = "'" + fname + "'". FYI on either side, it's a single quote surrounded by double quotes. Then type formula as the...formula: processing.runalg("qgis:fieldcalculator", fname, 'Filename', 2, 50, 0, True, formula, path_res + "/"+ fname). This should give you the filename as values whilst retaining all the features. – Joseph Sep 11 '15 at 15:26
  • @EdRollason - Awesome buddy, glad it worked :) – Joseph Sep 14 '15 at 9:14

From your comment I would assume that you just want to add a new field?

Open the attribute table of the file, start an edit session, and then use this button:

enter image description here

In your case you would have to opt for Text:

enter image description here

  • To add a columb is not the problem. The comprtition is to put the name of the layer into the column. At least I want to do this witch the processing batch tool for more than just 2 files. – Pimpel Feb 3 '15 at 17:18

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.