I'm trying to load a directory of shapefiles into a geodatabase with ogr2ogr. It works fine if I specify a single shapefile, but can't open the datasources if I specify a wildcard. Is there a wildcard I can use to import all of the shapefiles in the directory?

this works:

ogr2ogr -f 'FileGDB' -update -append allroads.gdb roads.shp

This fails and errors with: "Unable to open datasource `roads.shp' with the following drivers."

ogr2ogr -f 'FileGDB' -update -append allroads.gdb *.shp
  • Might be easier to write a loop in whatever shell this is running from - is this Windows or Unix/Linux/Mac?
    – Spacedman
    Feb 22 '16 at 21:00
  • This is on a mac, so a loop in bash would work.
    – jamierob
    Feb 22 '16 at 21:01
  • 2
    Have you tried just giving the folder instead of a shapefile? OGR should treat the folder as a data source, and each shapefile as a layer. In a test, I created a GML with all shapefiles from a folder in one line: ogr2ogr -f GML foo.gml ./shapes/ - not sure if the FileGDB driver will work the same way...
    – Spacedman
    Feb 22 '16 at 21:14
  • 1
    @Spacedman - that is the ticket! worked like a charm. Will you post that as an an answer so I can mark it?
    – jamierob
    Feb 22 '16 at 21:38

OGR treats a folder as a data source with shapefiles in the folder as layers. If you add the folder using ogr2ogr then it will add all the shapefiles to the output data source. For example to create a GML file with one layer for every shapefile in the folder shapes, do:

ogr2ogr -f GML foo.gml ./shapes/

which should work for any OGR output data format, including your File GDB.

Note that it might add any geographic data files in ./shapes/, so if there's KML or GML in there they might get added too.

  • That's great info I never heard about before. Do you know if the exact behaviour is documented somewhere?
    – Kersten
    Feb 23 '16 at 12:09
  • 1
    Yes, in the shapefile driver docs gdal.org/drv_shapefile.html "Normally the OGR Shapefile driver treats a whole directory of shapefiles as a dataset, and a single shapefile within that directory as a layer."
    – Spacedman
    Feb 23 '16 at 12:23
  • AWESOME! very useful for my task!
    – Pil Kwon
    Jun 21 '18 at 14:23

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.