Using ogr2ogr to convert all shapefiles in directory?

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.


For unix bash:

#!/bin/bash

for FILE in *.mif # cycles through all files in directory (case-sensitive!)
do
    echo "converting file: $FILE..."
    FILENEW=`echo $FILE | sed "s/.mif/_new.shp/"` # replaces old filename
    ogr2ogr \
    -f "ESRI Shapefile" \
    "$FILENEW" "$FILE"
done
exit

If you're working in a *nix-based OS (ie Linux or OS X), there are some batch shell scripts that clhenrik developed here (which the above is based on).

I'm working on a fork that makes some of the scripts a little more generic and provides a little more description on use.



with python:

import os 

for a in os.listdir(os.getcwd()):
   fileName, fileExtension = os.path.splitext(a)
   os.system('ogr2ogr -f "ESRI Shapefile" %s %s' % (a, fileName + '.tab'))

you can change os.getcwd() to your path where your files are located...

i hope it helps you