Exporting several files at same time in QGIS?

Try this Frida:

  1. Create a folder to store your Shapefiles there (e.g., I've created the folder /tmp/data/, I use GNU/Linux).

  2. In QGIS, open the QGIS Python console.

  3. Write the following line, editing the right hand side to match the full path to your folder (make sure you include the trailing slash/backslash):

    myDir = '/tmp/data/'

  4. Press Enter.

  5. Copy the following lines to the QGIS Python console:

    for vLayer in iface.mapCanvas().layers():
        QgsVectorFileWriter.writeAsVectorFormat( vLayer, 
            myDir + vLayer.name() + ".shp", "utf-8", 
            vLayer.crs(), "ESRI Shapefile" )
    
  6. Press Enter a couple of times.

You should now have your Shapefiles inside the folder you created in step 1.

If you face troubles, let me know your OS and the full path to your folder.


from qgis.core import *

suffix = "_foo"
pathToFile = "/path/to/wherever/"

layers = iface.legendInterface().layers()
for layer in layers:
    newName = layer.name() + suffix + ".shp"
    ret = QgsVectorFileWriter.writeAsVectorFormat(layer, pathToFile + newName, "utf-8", None, "ESRI Shapefile")
    if ret == QgsVectorFileWriter.NoError:
        print newName + " saved to " + pathToFile + "!"

For Raster Layer use QgsRasterFileWriter

€: You can find a more detailed example in the Cookbook.


Just a little addition: If you would like to change the CRS of the exported file as well and add some more prefix and suffix you can alter the script like so:

from qgis.core import *
import os
pathToFile = "S:\\pathway\\"
trs = QgsCoordinateReferenceSystem()
trs.createFromId(31370)
suffix = "_Lambert1972_Versie2016-01-04"
prefix = "Transect_Vuursalamander_"
layers = iface.legendInterface().layers()
for layer in layers:
    newName = prefix + layer.name() + suffix + ".shp"
    ret = QgsVectorFileWriter.writeAsVectorFormat(layer,pathToFile + newName,'utf-8',trs,'ESRI Shapefile')
    if ret == QgsVectorFileWriter.NoError:
        print newName + " saved to " + pathToFile + "!"

The 31370 can be replaced by the EPSG of the CRS you would like the exported file to be in.