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).

OS is Windows 7. I try this in the command prompt: for %f in *.shp; do ogr2ogr -f MapInfo File $f.tab. But that doesn't work. I get the error: 'Unable to open datasour '$f.tab' with the following drivers.....
–
StefanMay 14 '12 at 9:46

This is how to do SHP to TAB: ogr2ogr -f "MapInfo File" %1.tab %1.shp. But unless OGR is able to recognize a PRJ file correctly, you easily end up with TAB files with the wrong coordsys built-in. I recommend using the built-in Universal Translator (scaled down FME), which can also process all the files you select. It allows you to select the correct coordsys.
–
Uffe KousgaardMay 14 '12 at 12:01

I do indeed have problems with the right projection in MapInfo File. I try to include the right projection, but this doesn't work. The projection in de mapinfo file is set to non-earth (meters). Is it possible to include the correct projection with the ogr2ogr tool. I would like to put the command lines in another script. So I rather not use the Universal Translator.
–
StefanMay 14 '12 at 14:10

7 Answers
7

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.

Cool! This works! Many thanks for the answer :) One more question: Is there also a way to refer to subdirectory and convert the files in another subdirectory. I have a containing various directories containting shapefiles. Such as: Netherlands\shape\provinceX Netherlands\shape\provinceY Netherlands\shape\provinceZ And would like to convert this into these folders: Netherlands\mapinfo\provinceX Netherlands\mapinfo\provinceY Netherlands\mapinfo\provinceZ Thanks :)
–
StefanMay 14 '12 at 10:21

I've updated the above for a recursive search, however I don't know how to do the extra logic to change "shape" to "mapinfo"; maybe find/ask on superuser.com
–
Mike TMay 14 '12 at 11:06

1

Thanks again for your quick response. This was very helpful. I only need to copy the files to the right directory.
–
StefanMay 14 '12 at 12:22

NB This reply came from a closely related question that referred to different file formats.
–
whuber♦May 16 '12 at 14:07

Hi, when I try this approach in Ubuntu e.g. ogr2ogr -f "KML" {outfolder} {infolder} I get the error: 'ERROR 4: Failed to create KML file'. Any idea what might be going wrong here? I have built GDAL with KML support
–
marty_cFeb 23 at 12:10

Thanks! Can I refer from one parent directory to all the subdirs and converting all the files to other subdirs. Ihave: Netherlands\shape\provinceX Netherlands\shape\provinceY Netherlands\shape\provinceZ and so on... And would like to convert this into these folders: Netherlands\mapinfo\provinceX Netherlands\mapinfo\provinceY Netherlands\mapinfo\provinceZ I can write a line for each province, but can I also do it with one command
–
StefanMay 14 '12 at 12:24

I havent't tried this, but I don't think that this is possible. You would need a loop wich handles the respective input and output directories. In this case it is probably easier to follow a python approach.
–
TorstenMay 15 '12 at 9:54