OGR2OGR – How to Convert All Shapefiles in a Directory

convertmapinfoogr2ogrshapefile

I have a directory with several shapefiles.

How can convert all these shapefiles to MapInfo with ogr2ogr?

I know how I can convert one file.
And I can make a batch script writing a line for each file.
But isn't there an easier way to convert all the files in a directory (and subdirectory).

Best Answer

On Windows, for the current and sub-directories under the current, try this command:

for /R %f in (*.shp) do ogr2ogr -f "MapInfo File" "%~dpnf.tab" "%f"

To briefly explain the trickery of what is going on here, %~dpnf.tab uses the variable %f, with which it adds the driver letter, path name (i.e., folder or directory), and extracts the file name (without the .shp file extension). Lastly, .tab is added immediately after the compound variable modifiers for the new extension.

So if you are in directory C:\MyData, and you have data in this directory, and sub-directories C:\MyData\Region1 and C:\MyData\Region1\City1, any Shapefile (with .shp extension) will be processed, and a similar named file with .tab will created in the same directory.