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?

1 Answer 1


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"


This one works for me:

for /R %%F in (*.shp) do ogr2ogr -f "KML" "%%~dpnF.kml" "%%F"
  • Hi Andre, thanks for the help, but it still comes up with 'unable to open datasource' issue (for both cases), even for files without spaces. I re-tested with my old code (which I have delightfully stolen from another topic) and it still works for names without spaces. the code is: for /R %%f in (*.shp) do ogr2ogr -f "KML" %%~dpnf.kml %%f
    – Drew Win
    Commented Sep 3, 2013 at 4:10
  • Is there anything I can do to the code immediately above to accommodate fpor spaces in file names?
    – Drew Win
    Commented Sep 3, 2013 at 4:13
  • I tried, for /R %f in (".shp") do ogr2ogr -f "KML" "%~dpnf.kml" %f , and I got the same error. Also tried for /R %f in (".shp") do ogr2ogr -f "KML" %~"dpnf.kml" %f. Still same unfortunately.
    – Drew Win
    Commented Sep 3, 2013 at 4:21
  • Just one quotation still missing. See my extended answer.
    – AndreJ
    Commented Sep 3, 2013 at 8:27
  • Excellent!! Thank you very much for that! Now it does the conversion, but gave me a warning "Layer name ‘WP Waterbodies_WGS84’ adjusted to ‘WP_Waterbodies_WGS84’ for XML validity". Would I be correct in assuming that this is normal?
    – Drew Win
    Commented Sep 3, 2013 at 22:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.