Tuesday 26 April 2016

mapinfo - Using ogr2ogr to convert all shapefiles in directory?


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



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.


No comments:

Post a Comment

arcpy - Changing output name when exporting data driven pages to JPG?

Is there a way to save the output JPG, changing the output file name to the page name, instead of page number? I mean changing the script fo...