There is a nice way to upload all the layers from one folder in QGIS. Is there a way to download all the layers from a project and save them in a new folder?
1 Answers
An easy way would be via the QGIS Python Console (Plugins > Python Console, then click on the Show Editor icon in the left console toolbar), and then entering this code, only changing the directory you wish to save the files:
import ogr,os
myDir = 'C:/Shapefiles/'
if os.path.exists (myDir) == False:
print("Path does not exist")
else:
for vLayer in iface.mapCanvas().layers():
if vLayer.type()==0: #Save only shapefiles in the Layer Panel
QgsVectorFileWriter.writeAsVectorFormat(vLayer, myDir + vLayer.name() + ".shp", "utf-8", vLayer.crs(), "ESRI Shapefile")
print(vLayer.name() + " saved successfully")
Just make sure that the variable myDir includes the folder name followed by an additional / (as in the example above), and that the folder exists before running the script. Don't use traditional Windows back slashes \ in the path as this will fail; use forward slashes / as shown.
The script will only save the active (checked) shapefiles in the layer control panel (ignoring other file formats).
-
1Love it, Thanks 15Step! Just had to make a small change to make it work for me. The last line changed to : print (" saved successfully", vLayer.name()) – Phil_in_Tx Mar 07 '19 at 21:28
-
1This looks like a great solution, but it did not work for me. I get the following: Traceback (most recent call last): File "C:\OSGEO4~1\apps\Python37\lib\code.py", line 90, in runcode exec(code, self.locals) File "", line 1, in
File " – Mark Thompson Sep 10 '19 at 19:35", line 2 myDir = 'C:\Shapefiles' ^ SyntaxError: EOL while scanning string litera
