[GIS] Handling file names with spaces in ogr2ogr

batchconvertogr2ogrshapefile

I'm trying to convert .shp files to .kml files. I used a batch file to find all .shp files in a directory and its sub-directories and convert them to .kml. Some converted and some gave me the error "Unable to open datasource with the following drivers". I worked out that it was not converting due to spaces existing in the file names. When I got rid of spaces in the file name, it converted.

With that said, I need the files to be the same name so that they can be referenced by other programs correctly. Is there a way either in ogr2ogr or in windows cmd line to accommodate for spaces in files names when being batch converted?

Best Answer

Two possibilities:

  • Use wildcards to process all files in a directory:

FOR %%F in (D:\Karten\shp\Gemeinden\*.shp) DO ogr2ogr -t_srs EPSG:31466 D:\Karten\shp\neu\%%~nxF %%F

  • Use quotation marks around path and filename:

ogr2ogr -t_srs EPSG:31466 "D:\Karten\shp\Test 1.shp" "D:\Karten\shp\Test 2.shp"

EDIT

This one works for me:

for /R %%F in (*.shp) do ogr2ogr -f "KML" "%%~dpnF.kml" "%%F"